Tharanga Abeyseela
Tharanga Abeyseela

Reputation: 3483

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.

Upvotes: 1

Views: 17222

Answers (6)

Tharanga Abeyseela
Tharanga Abeyseela

Reputation: 3483

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!")

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193058

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

Upvotes: 1

iamsankalp89
iamsankalp89

Reputation: 4739

You can use execute_script

driver.execute_script("document.getElementById('button-1111-btnInnerEl').click()")

Upvotes: 3

Manmohan_singh
Manmohan_singh

Reputation: 1804

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

Upvotes: 0

Zakaria Shahed
Zakaria Shahed

Reputation: 2697

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']")

Upvotes: 0

Amit
Amit

Reputation: 20456

driver.find_element_by_id("button-1111-btnInnerEl").click()

Upvotes: 1

Related Questions