Home > Mobile >  Unable to locate element using Selenium Python find element by xpath
Unable to locate element using Selenium Python find element by xpath

Time:10-05

I have input:

<input type="text"  kpi-chitieu="001" id="txt-danhgia-001" value="0">

I use Selenium with python to select input. My code is:

button = driver.find_element(by=By.XPATH, value="//input[@id='txt-danhgia-001']")
u = '100'
button.send_keys(u)

But I can not locate input. I've got an error message:

NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@id='txt-danhgia-001']"}
  (Session info: chrome=105.0.5195.127)

Please help to how to select this input. Thank you very much.

CodePudding user response:

Try this:

driver.find_element(By.ID, 'txt-danhgia-001')

You could also shorten your code to:

driver.find_element(By.ID, 'txt-danhgia-001').send_keys('100')

CodePudding user response:

@Richard Nguyen, Since I do not know the url but may be the element is taking time to load. So it is good to wait for its visibility.

Try this:

button  = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//input[@id='txt-danhgia-001']")))

instead of :

button = driver.find_element(by=By.XPATH, value="//input[@id='txt-danhgia-001']")

in your code.

You will need the following import for it to work:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

If this does not work then other possibilities may be element is inside an iframe/frame. In that case, you need to first switch to the frame using driver.switch_to.frame(frametoswitchto)

  • Related