简体   繁体   English

Selenium即使存在也找不到元素

[英]Selenium can't find elements even if they exist

I'm creating a simple selenium script to enter username and password to log in. Here is my code:我正在创建一个简单的 selenium 脚本来输入用户名和密码以登录。这是我的代码:

driver = webdriver.Chrome(executable_path=r'C:\\Users\\Aspire5\\Downloads\\chromedriver_win32\\chromedriver.exe')
driver.get("https://ven02207.service-now.com/")

username = driver.find_element_by_xpath('//*[@id="user_name"]')
username.send_keys('username')

password = driver.find_element_by_xpath('//*[@id="user_password"]')
password.send_keys('this_is_password')

But I'm getting following exception:但我收到以下异常:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="user_name"]"}

I'm accessing this website from code.我正在从代码访问这个网站。 The XPath I've provided in code are exists on the page, but still it's returning No Such Element Exception .我在代码中提供的 XPath 存在于页面上,但它仍然返回No Such Element Exception

What I'm missing here?我在这里缺少什么? I've seen this , this questions for this, but couldn't find exact answer.我看过这个这个问题,但找不到确切的答案。

you need to first switch to the frame.. since the input tag is inside the frame你需要先切换到框架..因为input标签在框架内

frame = driver.find_element_by_xpath('//*[@id="gsft_main"]')
driver.switch_to.frame(frame)
driver.find_element_by_id('user_name').send_keys('sarthak')
driver.find_element_by_id('user_password').send_keys('sarthak')

You need to wait for element to present in DOM.您需要等待元素出现在 DOM 中。 So try to wait before getting web element driverwebdriver.Chrome(executable_path=r'C:\Users\Aspire5\Downloads\chromedriver_win32\chromedriver.exe')所以在获取web元素驱动webdriver.Chrome之前尽量等待(executable_path=r'C:\Users\Aspire5\Downloads\chromedriver_win32\chromedriver.exe')

driver.get("https://ven02207.service-now.com/")

username = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, "//*[@id="user_name"]"))
username.send_keys('username')

password = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, '//*[@id="user_password"]'))
password.send_keys('this_is_password')    

Just use escape slashes and you'll be fine只需使用转义斜杠就可以了

'//*[@id=\"user_name\"]'
'//*[@id=\"user_password\"]'

I'm creating a simple selenium script to enter username and password to log in. Here is my code:我正在创建一个简单的 selenium 脚本来输入用户名和密码进行登录。这是我的代码:

driver = webdriver.Chrome(executable_path=r'C:\\Users\\Aspire5\\Downloads\\chromedriver_win32\\chromedriver.exe')
driver.get("https://ven02207.service-now.com/")

username = driver.find_element_by_xpath('//*[@id="user_name"]')
username.send_keys('username')

password = driver.find_element_by_xpath('//*[@id="user_password"]')
password.send_keys('this_is_password')

But I'm getting following exception:但我收到以下异常:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="user_name"]"}

I'm accessing this website from code.我正在通过代码访问这个网站。 The XPath I've provided in code are exists on the page, but still it's returning No Such Element Exception .我在代码中提供的 XPath 存在于页面上,但它仍然返回No Such Element Exception

What I'm missing here?我在这里缺少什么? I've seen this , this questions for this, but couldn't find exact answer.我已经看到了这个这个问题,但找不到确切的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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