简体   繁体   中英

Message: no such element: Unable to locate element error finding an element using Xpath Selenium and Python

Here is the element I am trying to click on for an automated program using Selenium:

<input id="btnNextWeek" title="Next Week" onclick="if (this.className != 'mybtndis2') { moveweek(7) }" type="button" value=">>" name="btnNextWeek" class="mybtn2" style="-webkit-appearance: button; padding-left: 8px; padding-right: 8px; height: auto">

Getting this error:

Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@title="Next Week"]"} (Session info: chrome=83.0.4103.116)

This is the code that gives the error:

self.driver.find_element_by_xpath("//input[@title=\"Next Week\"]")\
.click()

Any suggestions?

Use this - driver.find_element_by_xpath('//input[@title="Next Week"]').click()

This will work.Always if you are using double quotes inside use single quote before to get rid of the escaping and we dont need to escape the. before click() because here. is used to call a method.

Thanks.

The desired element is a JavaScript enabled element so to click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :

  • Using CSS_SELECTOR :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.mybtn2#btnNextWeek[title='Next Week'][name='btnNextWeek']"))).click()
  • Using XPATH :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='mybtn2' and @id='btnNextWeek'][@title='Next Week' and @name='btnNextWeek']"))).click()
  • Note : You have to add the following imports:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC

References

You can find a couple of relevant discussions on no such element in:

It could be the element cannot be found because it is inside an iframe... if so then you could use

frame = driver.find_element_by_xpath('insert frame xpath')
driver.switch_to.frame(frame)
driver.find_element_by_xpath('//input[@title="Next Week"]').click()
driver.switch_to.default_content()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM