This is the html I got and I'm trying to select this fsuser and use send_keys to input some value. I tried this:
input_username = driver.find_element_by_xpath("//table/tbody/tr[2]/td/form/table/tbody/tr[1]/td[2]/input")
But is says:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//table/tbody/tr[2]/td/form/table/tbody/tr[1]/td[2]/input"}
(Session info: chrome=99.0.4844.74)
CodePudding user response:
You can directly use name
attribute, no need for the absolute xpath.
try this:
Css:
input[name='fsuser']
use it like this:
input_username = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[name='fsuser']")))
input_username.send_keys('some name')
OR
xpath:
//input[@name='fsuser']
use it like this:
input_username = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[@name='fsuser']")))
input_username.send_keys('some name')
Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Update:
NoSuchElementException:
Please check in the dev tools
(Google chrome) if we have unique entry in HTML-DOM
or not.
xpath that you should check :
//input[@name='fsuser']
Steps to check:
Press F12 in Chrome
-> go to element
section -> do a CTRL F
-> then paste the xpath
and see, if your desired element
is getting highlighted with 1/1
matching node.
If this is unique //input[@name='fsuser']
then you need to check for the below conditions as well.
Check if it's in any
iframe/frame/frameset
.Solution: switch to iframe/frame/frameset first and then interact with this web element.
Check if it's in any
shadow-root
.Solution: Use
driver.execute_script('return document.querySelector
to have returned a web element and then operates accordingly.Make sure that the element is rendered properly before interacting with it. Put some
hardcoded delay
orExplicit wait
and try again.Solution:
time.sleep(5)
orWebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='vaadin-text-field-container']//descendant::input[@part='value' and starts-with(@aria-labelledby, 'vaadin-text-field-input')]"))).send_keys("test")
If you have redirected to a
new tab/ or new windows
and you have not switched to that particularnew tab/new window
, otherwise you will likely getNoSuchElement
exception.Solution: switch to the relevant window/tab first.
If you have switched to an iframe and the new desired element is not in the same iframe context then first
switch to default content
and then interact with it.Solution: switch to default content and then switch to respective iframe.