简体   繁体   中英

Unable to locate element using Selenium Python find element by xpath

I have input:

<input type="text" class="cell-danhgia" 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.

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

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

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