简体   繁体   English

无法使用 xPath 定位元素

[英]Unable to locate element with xPath

I'm literally going crazy to find an element in a specific web page.我真的要疯了才能在特定的网页中找到一个元素。 It's an "Enter" button but I haven't been being able to locate it.这是一个“输入”按钮,但我一直无法找到它。

ERROR MESSAGE:错误信息:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"input"}

(Session info: chrome=86.0.4240.111) (会话信息:chrome=86.0.4240.111)

I'm going to share with you the possible selectors by ChroPath Extension:我将通过 ChroPath Extension 与您分享可能的选择器:

ChroPath选择器

ChroPathSelector(1)

HTML INSPECT CODE "ENTER" BUTTON: HTML 检查代码“输入”按钮:

<input type="button" value="Entra" onclick="parent.location.href='http://maremt.risorse.enel'">

1°ATTEMPT - PYTHON CODE WITH REL XPATH: 1°尝试 - 带有 REL XPATH 的 Python 代码:

elem = browser.find_element_by_xpath('//body[1]/div[1]/div[1]/div[2]/div[1]/table[1]/tbody[1]/tr[1]/td[2]/table[1]/tbody[1]/tr[3]/td[1]/input[1]')

2°ATTEMPT - PYTHON CODE WITH ABS XPATH: 2°尝试 - 带有 ABS XPATH 的 Python 代码:

elem = browser.find_element_by_xpath('/html[1]/body[1]/div[1]/div[1]/div[2]/div[1]/table[1]/tbody[1]/tr[1]/td[2]/table[1]/tbody[1]/tr[3]/td[1]/input[1]')

I have checked if there were any iframes, but I can't find them.我检查过是否有任何 iframe,但我找不到它们。 Please, help me.请帮我。

I agree with @josifoski.我同意@josifoski。 It is better to use custom xpath than autogenerated by browser.使用自定义 xpath 比由浏览器自动生成更好。 So in your case try to use the following xpath: xpath = '//input[@type="button"][@value="Entra"]'因此,在您的情况下,请尝试使用以下 xpath: xpath = '//input[@type="button"][@value="Entra"]'

It will be easier to understand and support.会更容易理解和支持。

I got it.我知道了。

I was wrong when I told you that there weren't any iframes in the source code.当我告诉你源代码中没有任何 iframe 时,我错了。

There are 2 ones!有2个!

So this is the right code to click the "Enter" button:所以这是单击“Enter”按钮的正确代码:

browser.switch_to.frame(0)
browser.switch_to.frame(1)
elem = browser.find_element_by_css_selector('td:nth-child(2) input')
elem.click()

To figure out, I recorded my steps with Selenium Ide for Chrome Browser, then I exported the python source code and I saw 2 switch_to.frame functions;为了弄清楚,我用 Chrome 浏览器的 Selenium Ide 记录了我的步骤,然后我导出了 python 源代码,我看到了 2 个 switch_to.frame 函数; then I did 2 plus 2...然后我做了2加2...

Like you can see, at the end, I used a css_selector argument based on the recorded source code that I had exported.如您所见,最后,我使用了基于我导出的记录源代码的 css_selector 参数。

I hope it's clear and useful for other people.我希望它对其他人来说是清晰和有用的。

Thank you so much for your help and sorry for the incomplete information.非常感谢您的帮助,对于不完整的信息,我们深表歉意。

你能试试这个吗

xpath = '//input[@value="Entra"]'

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

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