【发布时间】:2021-03-02 13:34:58
【问题描述】:
这是我试图点击的链接的 html 代码(我不知道为什么 selenium 无法找到链接。我的猜测是,有 X 数量的链接。唯一的区别是括号中的字符串onclick。)在下面,我将向您展示 2 个 html 代码示例。我想点击所有这些(在这种情况下同时点击)!
-
按钮或链接:
<td class="text-right"> <a href="#"onclick="doRetrievePnr('DUA75J')" id="viewPnr">Ansehen</a></td> -
按钮或链接:
<td class="text-right">
<a href="#" onclick="doRetrievePnr('C574DC')" id="viewPnr">Ansehen</a></td>
这是我点击按钮的尝试:
driver.find_element_by_link_text("doRetrievePnr('DUA75J')").click()driver.find_element_by_xpath("//a[@onclick='doRetrievePnr('DUA75J')']").click()
这是我得到的错误:
-
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"doRetrievePnr('DUA75J')"} -
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //a[@onclick='doRetrievePnr('DUA75J')'] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//a[@onclick='doRetrievePnr('DUA75J')']' is not a valid XPath expression.
我错过了什么吗?
编辑 Heres a picture of the BUTTONS, the HTML CODE and my PYTHON CODE (lines 34 & 35)
【问题讨论】:
-
By_Link_text是链接文本,这是标签之间的位,即Ansehen...您的 xpath 您遇到了引用问题。看起来单引号太多了,我预计它会在中途终止。很高兴查看页面并反馈答案,您可以分享指向它的链接吗? -
@RichEdwards 我必须登录该网站才能看到它。但我会尝试用 html 代码截取页面!请稍等。
-
如果你想点击所有这些,你可以做
find_elements(注意它是复数) - 并使用 idviewPnr- 然后遍历结果以与每个交互 -
@RichEdwards 我添加了更详细的图片!与此同时,我将尝试复数方法。我以前试过,但这次我可以做到。
标签: html python-3.x selenium selenium-chromedriver