Send_keys function triggers error message: 'Message: element not interactable' - python

I'm using Selenium to fill out this HTML form, but when it comes to inputting the data it says 'element not interactable'. I am able to click on the element however actually sending a string produces an error. How can I fix this?
driver.get('https://www.masmovil.es/cobertura-fibra-optica-campos/')
prov = Select(driver.find_element_by_xpath('//*[#id="province"]'))
prov.select_by_index(32)
driver.find_element_by_xpath('//*[#id="town"]').send_keys('1')
Thank you!

In the page you are accessing there are 2 elements that are returned with the selector by_xpath('//*[#id="town"]'), one is a "mm-ui-autocomplete", the other one is an "input".
the "mm-ui-autocomplete" is not visible nor interactable to a real user, that's probably what's throwing the exception you're having, and selenium always takes the first match when there's more than one element returned by the selector, so, assuming you want to type something on the "Localidad" field, it is selecting the wrong element.
Try changing your selector to by_xpath('//input[#id="town"]') and see if it works.
Hope it helps.

Can you try with this css selector :
input[id='town']
code :
driver.find_element_by_css_selector("input[id='town']").send_keys('1')
The xpath (//*[#id="town"]) you have used has two entries :
one with mm-ui-autocomplete tag and one with input tag.
Always give preference to css selector over xpath. It's more stable then xpath.
In case you would not want to use css selector, then you can use xpath like this :
//input[#id='town']
Code :
driver.find_element_by_xpath("//input[#id='town']").send_keys('1')

In my case, it happens that the find_element was not working before the frontend finished loading.
I solved this by adding sleep(2) before the find_element_by_xpath. You will need to import the function by from time import sleep.

Related

InvalidSelectorException Error while trying to get text from div class in Selenium Python

I'm trying to get text using Selenium WebDriver and here is my code. Please note that I don't want to use XPath, because in my case the ID gets changed on every relaunch of the web page.
My code:
driver.find_element_by_class_name("05uR6d").text
HTML:
<div class="O5uR6d">to fasten stuff</div>
Error:
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified (Session info: chrome=88.0.4324.150)
Error is specific to the line of code I mentioned above.
How can I fix this?
Use this xpath:
driver.find_element_by_xpath("//div[contains(text(),'to fasten stuff')]")
Or this CSS:
driver.find_element_by_css_selector(".O5uR6d")
If both won't work, improve your question by adding more data of HTML you are looking at.
It can be done using multiple ways let me try to explain most of them.
Get element by class name.
this is the most easiest solution to get any element by class name you can simply do is:
driver.find_element_by_class_selector('foo');
Get Element by xpath
This is a bit tricky one, you can apply xpath either the class name, title, id or whatever remains same. it also works even if there's a text inside your div. For example:
driver.find_element_by_xpath("//tagname[#attribute='value']")
or in your case:
driver.find_element_by_xpath("//div['class='O5uR6d']")
or you can do something like #vitaliis said
driver.find_element_by_xpath("//div[contains(text(),'to fasten stuff')]")
You can read more about xpath and how to find it on this link
Get Elements by ID:
You can also get the element from id if there's any that's static:
driver.find_element_by_id('baz')
Get Elements by Name:
Get Elements by name using the following syntax:
driver.find_element_by_name('bazz')
Using CSS Selectors:
You can also use the css selectors to find the elements. Consider a following tag that has some attributes:
<p class="content">Site content goes here.</p>
You can get this element by:
driver.find_element_by_css_selector('p.content')
You can read more about it over here

what to do for dynamically changing xpaths in python using selenium?

I have a xpath as:
//*[#id="jobs-search-box-keyword-id-ember968"]
The number 968 constantly keeps on changing after every reload.
Rest of the string remains constant.
How to I find the constantly changing xpath?
You can use partial id with contains()
//*[contains(#id, "jobs-search-box-keyword-id-ember")]
You can try using starts-with below,
//*[starts-with(#id,'jobs-search-box-keyword-id-ember')]
The details provided is insufficient to to provide the accurate result. Still you can follow the below code references
In //*[#id="jobs-search-box-keyword-id-ember968"] the last number 968 keeps changing. but if you make this like //*[starts-with(#id,'jobs-search-box-keyword-id-ember')] then there might be possibility that you can have more then one element with the same partial is i.e. jobs-search-box-keyword-id-ember in this case it will locate on 1st matching element. that may not be your expected one
Use the tag name lets say element is an input tag whose id is jobs-search-box-keyword-id-ember968
Xpath - //input[starts-with(#id,'jobs-search-box-keyword-id-ember')]
CSS - input[id^='jobs-search-box-keyword-id-ember']
Use the relevant parent element to make this more specific. e.g the element is in parent tag <div class="container">
Xpath- //div[#class='container']//input[starts-with(#id,'jobs-search-box-keyword-id-ember')]
CSS - div.container input[id^='jobs-search-box-keyword-id-ember']
This worked for me:
Locator:
JOBS_SEARCH_BOX_XPATH = "//*[contains(#id,'jobs-search-box-keyword-id-ember')]"
Code:
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, JOBS_SEARCH_BOX_XPATH)))
.send_keys("SDET")

Click on li element searching specific displayed text

Using python 3 and chrome driver. I'm trying to click on my desired element searching for the text displayed on this page . For example, in case of "BEBES" I'm using:
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'//*[contains(text(), "BEBES")]'))).click()
but nothing happens. Just throws the time out exception. What's my error?
Your xPath is not correct. Use this:
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'//span[contains(text(), "Bebes")]'))).click()
Note: upper/lowercase makes difference
and
This post suggests using the following as text() returns a node set:
//*[text()[contains(.,'BEBES')]]
XPath contains(text(),'some string') doesn't work when used with node with more than one Text subnode

Selenium click on XPath button

I have the following code when I inspect on Chrome.
<span id="button-1111-btnInnerEl" class="x-btn-inner x-btn-inner-center" unselectable="on" style="">New Email</span>
I need to click on the label "New Email", but how should invoke it in Selenium (I'm using Python).
def CreateMail():
EmailButton="//*[contains(text(),'New Email')]"
driver.find_elements_by_xpath(EmailButton) // there is no method to enable click.
You can use execute_script
driver.execute_script("document.getElementById('button-1111-btnInnerEl').click()")
driver.find_element_by_id("button-1111-btnInnerEl").click()
Thanks all for your help. Finally i found the answer to my question.I had to add a wait statement, before finding the key. key wasn't present when the page loads, so had to wait a little bit to find the correct key.
def CreateMail():
try:
element = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID, "button-1143-btnInnerEl")))
driver.find_element_by_id("button-1143-btnInnerEl").click()
except TimeoutException:
print ("Loading took too much time!")
Hope This XPath will work for you.If you want to validate xpath using your chrome browser just paste this text on your chrome console $x("//*[text()='New Email']") and check how many elements found using this XPath
driver.find_elements_by_xpath("//span[text()='New Email']")
Your statement : "driver.find_elements_by_xpath(EmailButton)" . Click does not work on group of elements. It is actionable only on single element. So you use a singular finder.
driver.find_**element**_by_id(EmailButton).click()
As per the HTML you have shared, the id attribute looks dynamic to me. So we must construct a dynamic xpath or css. Additionally instead of find_elements we have to use find_element so a single WebElement is returned and we can invoke the click() method. Finally, if you look at the node properly, the unselectable attribute is on so we will take help of JavascriptExecutor as follows :
myElement = driver.find_element_by_xpath("//span[starts-with(#id, 'button-')][#class='x-btn-inner x-btn-inner-center']")
driver.execute_script("arguments[0].click();", myElement);
Actually there is different way for locating elements
but in your case there is a ID,
so you can prefer id if its exists
find solution below :
driver.find_element_by_id("button-1111-btnInnerEl").click()

Can't get the value of an attribute with Selenium RC using xpath

I'm trying to get the first href attribute in a page using Selenium RC (in Python):
sel.get_text("xpath=//#href")
this returns an empty string.
However, an identical xpath on the same page inside Firefox (using the "View XPath" extension) yields the correct value.
I've tried fiddling with it, but the same happens for other attributes (eg #class) -- is there something awfully wrong with selenium or am I overlooking something trivial here?
Solved by using selenium's get_attribute e.g. sel.get_attribute("xpath=//a#href") for a nodes.
In Selenium RC, you can use the get_attribute function as below.
AttrValue = sel.get_attribute("//li[#id='result_0']/div/div[3]/div/a#href")
where //li[#id='result_0']/div/div[3]/div/a is the xpath.
xpath= is not required inside the function.
I think this applies to all types of elements:
For example: for an input type of element
selenium.getAttribute("//input#value");

Categories