简体   繁体   English

在 Python 中使用带有 Selenium 的 webdriver 时找不到元素

[英]Element not found when using webdriver with Selenium in Python

got a problem with my code and I need your help.我的代码有问题,我需要你的帮助。 What I'm trying to do is the following:我正在尝试做的是以下内容:

1- access a website; 1-访问网站;

2- fill the registration form: name , email , password , etc. 2-填写注册表: nameemailpassword等。

Step 1 works;第 1 步有效; after clicking the sign up button, the form will pop up in a new tab.单击注册按钮后,表单将在新选项卡中弹出。

Step 2;第2步; when trying to find the elements, by, id or name , I get the error "selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element"当试图通过idname查找元素时,我收到错误"selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element"

My code is the following:我的代码如下:

driver.find_element_by_link_text('Sign Up').click()

time.sleep(3)

driver.find_element_by_id("signup_title").send_keys(signup_tile)
driver.find_element_by_id("signup_name").send_keys(signup_name)

Attached you can find the html.随附您可以找到 html。 Thank you in advance, much appreciated your help.提前感谢您,非常感谢您的帮助。 Note笔记

In console I tried to find the element searching the id using $x("//*[@id='signup_title']") and it was found: [input#signup_title.sc-AxirZ.kzqQJb.invalid] .在控制台中,我尝试使用 $x("//*[@id='signup_title']") 查找搜索id的元素$x("//*[@id='signup_title']") and it was found: [input#signup_title.sc-AxirZ.kzqQJb.invalid] Also the element is loaded.元素也被加载。

HTML HTML

Try using:尝试使用:

driver.find_element_by_xpath("<XPath>")

It seems to work better.它似乎工作得更好。 To get the XPath of an element, find the element in the inspector, right-click it then Copy -> Copy XPath.要获取某个元素的 XPath,请在检查器中找到该元素,右键单击它,然后复制 -> 复制 XPath。 If that doesn't work, select Copy Full XPath instead如果这不起作用,请 select 复制完整 XPath 代替

The problem was related to the second tab;问题与第二个选项卡有关; because the form opens in another tab, the window must be switched in the code.因为表单在另一个选项卡中打开,所以必须在代码中切换 window。 I used the following: driver.switch_to.window(driver.window_handles['Nr']), Nr being the index for the tab- if there are 2, the main one, and the second one-in my case with the form, the index will pe 1-counting starts from zero.我使用了以下内容: driver.switch_to.window(driver.window_handles['Nr']), Nr 是选项卡的索引 - 如果有 2 个,主要的,第二个 - 在我的表格中,索引将 pe 1-counting 从零开始。

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

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