为什么即使我从浏览器中复制它,为什么还能使用此XPath表达式找到它?

发布于 2025-01-24 15:05:18 字数 775 浏览 0 评论 0 原文

我很难单击特定的链接。我尝试使用 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()

I'm having a hard time clicking a particular link. I tried using find_element_by_css_selector and find_element_by_xpath. Both give me the following error, even though I copied the XPath expression from the browser. I thought maybe I needed to wait for the page to load, so I added a 30 second timer, but it still couldn't find the element.

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

HTML:

Enter image description here

Code:

time.sleep(30)

link = driver.find_element_by_xpath("//*[@id='ctl00_cphMain_RadDock1455_C_ctl00_gridTimesheets_ctl00__0']/td[2]/a")
link.click()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缪败 2025-01-31 15:05:18

您在XPATH表达式中使用的ID, CTL00_CPHMAIN_RADDOCK1455_C_CTL00_GRIDTIMESHEETS_CTL00_CTL00_CTL00_0',本质上可能是动态的,这意味着在运行时,有些值可能会更改,您可能会得到 nosuchelement Ixpection

您可以使用 link_text partial_link_text 数据分析师的情况下, and> andains 锚定标签的唯一文本。

driver.find_element(By.LINK_TEXT, "Data Analyst").click()

通常,应该工作。但是,如果您正在寻找可靠的解决方案,则应进行明确的等待:

wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Data Analyst"))).click()

必须在下面导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

如果您正在寻找基于XPath的定位器,则可以

//tr[@class='rgRow']//td[2]/a[@title='Data Analyst' and text()='Data Analyst']

全部 使用,您可以确保它本质上是独一无二的。

检查的步骤:

按chrome→在chrome中按 f12 转到 element 节→做一个ctrl + f →。然后粘贴Xpath表达式,看看您所需的元素是否正在使用 1/1 匹配节点突出显示。

nosuchelementException:

请在 html dom 与否。

您应该检查的XPath表达式:

//tr[@class='rgRow']//td[2]/a[@title='Data Analyst' and text()='Data Analyst']

检查的步骤:

在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'] 然后您还需要检查以下条件。

  1. 检查它是否在任何 iframe/frame/frameet 中。

    解决方案:首先切换到iframe/frame/frameet,然后与此Web元素进行交互。

  2. 检查是否在任何 shadow-root

    解决方案:使用 driver.execute_script('return document.queryselector 已返回Web元素,然后相应地操作。

  3. 确保在交互之前正确呈现元素使用它。

    解决方案: 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”)

  4. 如果您已将其重定向到a new Tab/或New Window ,并且您没有切换 到该特定的 new Tab/new Window ,否则您将可能获取 nosuchelement 异常。

    解决方案:首先切换到相关窗口/选项卡。

  5. 如果您已切换到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 get NoSuchElement exception.

You can use link_text or partial_link_text in case Data Analyst is unique text wrapped inside the anchor tag.

driver.find_element(By.LINK_TEXT, "Data Analyst").click()

in general should work. However, if you are looking for a reliable solution, you should go for explicit waits:

wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Data Analyst"))).click()

You'd have to import below:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

In case you are looking for xpath-based locator, you can use

//tr[@class='rgRow']//td[2]/a[@title='Data Analyst' and text()='Data Analyst']

All 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:

//tr[@class='rgRow']//td[2]/a[@title='Data Analyst' and text()='Data Analyst']

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.

  1. Check if it's in any iframe/frame/frameset.

    Solution: Switch to the iframe/frame/frameset first and then interact with this web element.

  2. 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.

  3. 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) or

    WebDriverWait(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")

  4. 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.

  5. 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.

☆獨立☆ 2025-01-31 15:05:18

尝试:

time.sleep(30)

link = driver.find_element_by_xpath("//a[@title='Data Analyst']")
link.click()

Try:

time.sleep(30)

link = driver.find_element_by_xpath("//a[@title='Data Analyst']")
link.click()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文