我正在尝试使用 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/locating-elements.html#locating-elements
关于Python - Selenium 通过 href 定位元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33887410/