为什么即使我从浏览器中复制它,为什么还能使用此XPath表达式找到它?
我很难单击特定的链接。我尝试使用 find_element_by_css_selector
和 find_element_by_xpath
。即使我从浏览器中复制了XPath表达式,两者都会给我以下错误。我以为也许我需要等待页面加载,所以我添加了一个30秒的计时器,但仍然找不到该元素。
selenium.common.exceptions.nosuchelementException:消息:否这样 元素:无法找到元素: {“ method”:“ xpath”,“ selector”:“ //*[@ID ='ctl00_cphmain_raddock1455_c_ctl00_gridtimesheets_ctl00__0']/td [2]/a“}
html:
{“方法”:“ xpath”,“ selector”: “ /GF5QT.PNG“ rel =” nofollow noreferrer“>
代码: 代码:
time.sleep(30)
link = driver.find_element_by_xpath("//*[@id='ctl00_cphMain_RadDock1455_C_ctl00_gridTimesheets_ctl00__0']/td[2]/a")
link.click()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在XPATH表达式中使用的ID,
CTL00_CPHMAIN_RADDOCK1455_C_CTL00_GRIDTIMESHEETS_CTL00_CTL00_CTL00_0'
,本质上可能是动态的,这意味着在运行时,有些值可能会更改,您可能会得到nosuchelement Ixpection
。您可以使用
link_text
或partial_link_text
在数据分析师的情况下, and> andains 是锚定标签的唯一文本。通常,应该工作。但是,如果您正在寻找可靠的解决方案,则应进行明确的等待:
必须在下面导入:
如果您正在寻找
基于XPath的定位器
,则可以全部 使用,您可以确保它本质上是独一无二的。
检查的步骤:
按chrome→在chrome中按 f12 转到 element 节→做一个ctrl + f →。然后粘贴Xpath表达式,看看您所需的元素是否正在使用 1/1 匹配节点突出显示。
nosuchelementException:
请在 html dom 与否。
您应该检查的XPath表达式:
检查的步骤:
在Chrome中按 f12 in Chrome→转到
element
proce ctrl < ctrl < /kbd> + f →然后粘贴xpath表达式,然后查看,如果您的所需元素 用 1/1 <强调 /em>匹配节点。如果这是唯一的,则
// tr [@class ='rgrow'] // td [2]/a [@title ='data Analyst'和text()='data Analyst']
然后您还需要检查以下条件。检查它是否在任何
iframe/frame/frameet
中。解决方案:首先切换到iframe/frame/frameet,然后与此Web元素进行交互。
检查是否在任何 shadow-root 。
中
解决方案:使用
driver.execute_script('return document.queryselector
已返回Web元素,然后相应地操作。确保在交互之前正确呈现元素使用它。
解决方案:
time.sleep(5)
或webdriverwait(驱动程序,20).ultil(ec.visibility_of_element_located(((by.xpath,“ // tr [@class ='rgrow'])// td [2]/a [@title ='data分析师'和text()='data Analyst'])))。send_keys(“ test”)
如果您已将其重定向到a new Tab/或New Window ,并且您没有切换 到该特定的 new Tab/new Window ,否则您将可能获取
nosuchelement
异常。解决方案:首先切换到相关窗口/选项卡。
如果您已切换到iframe,而新的所需元素不在相同的iframe上下文中,则首先切换到默认内容,然后与之交互。
解决方案:切换到默认内容,然后切换到相应的iframe。
The id that you are using with your XPath expression,
ctl00_cphMain_RadDock1455_C_ctl00_gridTimesheets_ctl00__0'
, could be dynamic in nature, which means at run time some value may change, and you may getNoSuchElement exception
.You can use
link_text
orpartial_link_text
in case Data Analyst is unique text wrapped inside the anchor tag.in general should work. However, if you are looking for a reliable solution, you should go for explicit waits:
You'd have to import below:
In case you are looking for
xpath-based locator
, you can useAll in all, you've to make sure that it is unique in nature.
Steps to check:
Press F12 in Chrome → go to the element section → do a Ctrl + F →. Then paste the XPath expression and see if your desired element is getting highlighted with 1/1 matching node.
NoSuchElementException:
Please check in the Web development tools (Google Chrome) if we have unique entry in the HTML DOM or not.
The XPath expression that you should check:
Steps to check:
Press F12 in Chrome → go to
element
section → do a Ctrl + F → then paste the XPath expression and see, if your desired element is getting highlighted with 1/1 matching node.If this is unique,
//tr[@class='rgRow']//td[2]/a[@title='Data Analyst' and text()='Data Analyst']
then you need to check for the below conditions as well.Check if it's in any
iframe/frame/frameset
.Solution: Switch to the 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 or explicit wait and try again.
Solution:
time.sleep(5)
orWebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//tr[@class='rgRow']//td[2]/a[@title='Data Analyst' and text()='Data Analyst']"))).send_keys("test")
If you have redirected to a new tab/ or new window and you have not switched to that particular new tab/new window, otherwise you will likely get
NoSuchElement
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 the default content and then switch to the respective iframe.
尝试:
Try: