NoSuchElementException:没有这样的元素:无法找到元素
免责声明:十多年后我又重新开始编写脚本,因此对于这样一个基本问题提前表示歉意,但非常需要并感谢您的帮助。
我最近尝试寻找脚本来自动化我的求职,为此,我找到了一个脚本,可以帮助我登录求职门户并申请符合我条件的职位。
但我相信脚本没有更新,因为我在运行它时遇到错误:
NoSuchElementException:没有这样的元素:无法找到元素: {"method":"xpath","selector":"//*[@id="root"]/div[2]/div[2]/div/form/div[2]/input"} (会话信息:chrome=98.0.4758.109)
我相信这与以下代码行有关:
driver.get("https://www.naukri.com")
driver.find_element_by_xpath('//*[@id="login_Layer"]/div').click()
time.sleep(5)
driver.find_element_by_xpath('//*[@id="root"]/div[2]/div[2]/div/form/div[2]/input').send_keys("YOUR NAUKRI LOGIN ID")
driver.find_element_by_xpath('//*[@id="root"]/div[2]/div[2]/div/form/div[3]/input').send_keys("YOUR NAUKRI PASSWORD")
time.sleep(5)
driver.find_element_by_xpath('//*[@id="root"]/div[2]/div[2]/div/form/div[6]/button').click()
该脚本能够访问 Naukri.com(工作门户)并找到 login_Layer ID 的路径em>,这将打开登录侧边栏。但随后它无法找到用户名和密码字段。可能是因为网页变了。
我正在尝试检查页面元素并找到正确的 xpath,但没有运气。再说一次,我已经有十多年没有接触过 XML/HTML 或任何类型的 Web 开发了(并且一开始就是个新手),所以发现它很难。
任何指导或帮助将不胜感激。
期待您的答复。
提前致谢!
Disclaimer: I'm coming back to scripting after more than a decade so apologies in advance for such a basic question but help is much needed and appreciated.
I recently tried to find scripts to automate my job hunt, to that end I found a script that would help me login to a job portal and apply to jobs matching my criteria.
But I believe the script is not updated because I'm coming across an error when I'm running it:
NoSuchElementException: no such element: Unable to locate element:
{"method":"xpath","selector":"//*[@id="root"]/div[2]/div[2]/div/form/div[2]/input"}
(Session info: chrome=98.0.4758.109)
I believe this is in regard to the following lines of code:
driver.get("https://www.naukri.com")
driver.find_element_by_xpath('//*[@id="login_Layer"]/div').click()
time.sleep(5)
driver.find_element_by_xpath('//*[@id="root"]/div[2]/div[2]/div/form/div[2]/input').send_keys("YOUR NAUKRI LOGIN ID")
driver.find_element_by_xpath('//*[@id="root"]/div[2]/div[2]/div/form/div[3]/input').send_keys("YOUR NAUKRI PASSWORD")
time.sleep(5)
driver.find_element_by_xpath('//*[@id="root"]/div[2]/div[2]/div/form/div[6]/button').click()
The script is able to go to Naukri.com (the job portal) and find the path for login_Layer ID, which opens the login sidebar. But then it is not able to find the username and password field. Maybe because the webpage has changed.
I'm trying to inspect the page elements and find the right xpath but having no luck. Again, haven't touched XML/HTML or web development of any kind for over a decade (and was a novice to begin with) so finding it hard.
Any guidance or help will be really appreciated.
Looking forward to your answers.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,你的 XPath 已经过时了。
如果您使用的是 chromium 浏览器,请右键单击您想要 XPath 的元素,然后单击“检查”。如果您的 DevTools 尚未打开,请执行此操作两次。然后它将突出显示页面源中的元素。之后,右键单击突出显示的元素,复制并“复制 XPath”以更新脚本。
这种语法应该仍然有效,但现代指南希望您使用它:
Yeah your XPaths are outdated.
If you are on a chromium browser, right click on the element you want the XPath for and click inspect. If your DevTools are not yet open, do this twice. Then it will highlight your element in the page source. After that do a right click on the highlighted element, copy, and "Copy XPath" to update your script.
This syntax should still work, but modern guidelines want you to use this:
您需要使用 webdriver 等待并切换您的 xpath。
进口:
You'd want to use webdriver waits and switch up your xpaths.
Imports:
您的 XPATH 实际上是不正确的。
我建议您尽可能编写最具弹性的 XPath,因为 XPath 越长,它就会变得越脆弱。让 XPath 基于大型 HTML 标签层次结构通常不是一个好主意,因为如果中间引入了新标签(新的 div 标签),它就会中断。我相信这个应用程序也发生了什么。
为了避免这种情况,您可以使用下面的 xpath。
Your XPATHs are actually incorrect.
What I would suggest you do is to write the most resilient XPath possible since the longer the XPath, the more brittle it becomes. Having the XPath based on a big HTML tag hierarchy is usually not a good idea since if there are new tags introduced in between (a new div tag) it will break. Which I believe what happened to this application also.
To avoid this you can use below xpaths.