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
Related
Currently trying to get the data which is stored in this class using selenium and print it into console.
<h3 class="tiktok-dvof16-AuthorTitle e10yw27c0">allawi_9</h3>
I tried print(driver.find_elements_by_class_name("tiktok-dvof16-AuthorTitle e10yw27c0"))
I also tried adding .text at the end, however you cant do that on a list obj. (And the returned list is empty may I add.
However, it just returns an empty list. I'm unsure what I'm doing wrong. Any help would be great!
if the element value is unique on the page you can use
myelemnt = driver.find_element_by_name('Some text')
myelement.print()
if not you can use X-path for this like
myelement = driver.find_element_by_xpath('/html/body/div[1]/section/main/div/div[3]/article/div[1]/div/div[1]/div[1]/a/div/div[2]')
myelement.print()
if that doesnt worked use Full x-path
myelement = driver.find_element_by_xpath('//*[#id="react-root"]/section/main/div/div[2]/article/div[1]/div/div[1]/div[1]/a/div/div[2]')
myelement.print()
for getting the x-path/full x-path simple just right-click on the element on the google chrome developer tools and click on Copy then x-path / full x-path
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.
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()
I am using the following code using Python 3.6 and selenium:
element = driver.find_element_by_class_name("first_result_price")
print(element)
on the website it is like this
`website: span class="first_result_price">712
however if I print element I get a completely different number?
Any suggestions?
many thanks!!
"element" is a type of object called WebElement that Selenium adds. If you want to find the text inside that element, you have to say
element.text
Which should return what you're looking for, '712', albeit in string form.
I'm trying to get the XPATH for Code Generator field form (Facebook) in order to fill it (of course before I need to put a code with "numbers").
In Chrome console when I get the XPATH I get:
//*[#id="approvals_code"]
And then in my test I put:
elem = driver.find_element_by_xpath("//*[#id='approvals_code']")
if elem: elem.send_keys("numbers")
elem.send_keys(Keys.ENTER)
With those I get:
StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
What means wrong field name. Does anyone know how to properly get a XPATH?
This error usually comes if element is not present in the DOM.
Or may be element is in iframe.