Selenium click on XPath button - python

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()

Related

How can I find an html element with always changing id through XPATH?

I ve run into a problem with my web automation. I have a drop down menu with two clickable Options. the id (picture ref.) changes every time that I load onto the page due to the element being created manually each time beforehand.
The "-1" and "-0" definition stays the same tho. Is there any possible way to get xpath to only look onto the "-1" and "-0" in the id?
Current code:
folder = driver.find_element_by_xpath("//ng-select[#placeholder='Choose Key Set..']")
folder.click()
element_present = EC.presence_of_element_located((By.XPATH, "//div[#id='a0cb2db88cfe-1']"))
WebDriverWait(driver, timeout).until(element_present)
folder = driver.find_element_by_xpath("//div[#id='a0cb2db88cfe-1']")
folder.click()
Thanks in advance guys!
I still haven't figured out the proper way to locate the html element, but here is my solution over an offset click:
actions.move_to_element_with_offset(driver.find_element_by_xpath("//ng-select[#placeholder='Choose Key Set..']"), 0,0)
actions.move_by_offset(100, 81).click().perform()
Since it changes only a part of the id value, you can try to locate elements by constant value part.
Just try to use these CSS ones:
"[role='option'] [id*='-0']"
"[role='option'] [id*='-1']"
I hope below code should work, But I have not tried yet.
for -0 it should be - //div[1][#class='ng-option ng-star-inserted']
for -1 it should be - //div[2][#class='ng-option ng-star-inserted']
Please try this and let me know the status

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

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.

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

Using python selenium to click an element not visible

I am using python selenium on browser to fill in some form. I was trying to select an element in the drop-down list,
0
but if I try to find it by text using this script:
browser.find_element_by_link_text("0").click()
it result an error:
"unknown error: Element is not clickable at point (498, 612). Other element would receive the click: ..."
and if try to find it by class name:
browser.find_element_by_class_name("dropdown-toggle").click()
it result in another error: "element not visible"
is there any way I can click onto that drop-down list? Thanks very much.
I had a similar problem. You can execute a script to change the visibility of that element once you find it and then click it.
driver.execute_script("arguments[0].style.visibility = 'visible';",myElement)
myElement.click()
Try to find it by Xpath searching for partial class and text at the same time:
browser.find_element_by_xpath(//a[contains(#class, 'dropdown-toggle select') and contains(text(), '0')]).click();
you should get the element by xpath and then press it.
browser.find_element_by_xpath(xpath).
read here how to get the xpath: http://www.wikihow.com/Find-XPath-Using-Firebug
Thanks for the feedback, I've found the problem which was due to the new script being loaded by javascript as triggered by some clicks. I've created the following code to capture the exception and retrying until the element is ready:
while True:
try:
time.sleep(1)
browser.find_element_by_xpath("//div[#class='tckt']/a").click()
print("found")
break
except ElementNotVisibleException:
print("notvisible")
except WebDriverException:
print("clickduplicate")
I read it on articles says this happened a lot on radio button for Chrome webdriver, hope this helps.
Firstly click the parent element by finding it, using it's xpath->find the element you want to click within the parent element like the way you did. i.e
browser.find_element_by_link_text("0").click()
Hope this 2 steps will work for you. Otherwise please post the url with the code you've tried-It'll be easy to find out the issue.

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