Python - Selenium 通过 href 定位元素

标签 python html selenium

我正在尝试使用 selenium 为我的 Python 程序查找(并单击)特定页面。

我试过了

driver.findElement(By.cssSelector("a[href*='text']")).click();

driver.findElement(By.partialLinkText("text")).click();

使用 text 我要搜索的内容。这些不适用于错误

AttributeError: 'WebDriver' object has no attribute 'findElement'

我只能假设它不能找到元素,因为那是针对 Java,而不是 Python。

网站上链接的唯一区别因素是 href 属性。

所有其他标签都有重复。我无法 100% 猜出正确的链接,因此我还需要定位器是部分文本。

有没有办法开始这个?我可以使用其他程序吗?有人成功地做到了这一点吗?我尝试搜索,但甚至没有人问过这个问题。

最佳答案

你可以试试:

from selenium.webdriver.common.by import By
...
driver.find_element(By.PARTIAL_LINK_TEXT, 'text').click()

from selenium import webdriver
...
driver.find_element_by_partial_link_text("text").click()

编辑:对于 href 本身的部分文本搜索,请尝试:

from selenium import webdriver
...
driver.find_element_by_xpath("//a[contains(@href,'text')]").click()

来源:

http://selenium-python.readthedocs.org/faq.html?highlight=click#how-to-auto-save-files-using-custom-firefox-profile

http://selenium-python.readthedocs.org/locating-elements.html#locating-elements

关于Python - Selenium 通过 href 定位元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33887410/

相关文章:

python - 如何在 read_csv 中将非数字条目转换为 NaN

Python Selenium : How to get cookies and format them to use in an http request

java - RobotFramework 中的嵌套循环

java - 在 Java 中使用 Selenium WebDriver 下载 zip 文件 - 下载弹出窗口总是出现

c# - ASP.NET Web API session 数据

python - 将 bool 值列添加到包含变量和时间列的数据帧

python - 在 3D 数据中寻找多条路径

python - 如果发生这种情况,我该如何安装 PIL?

html - 只能在文本的上半部分单击按钮/超链接

html - 如何仅在选定元素上动态应用 CSS [Angular]