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

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

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

我在寻找的东西。这些不适用于错误
AttributeError: 'WebDriver' object has no attribute 'findElement'

我只能假设它找不到元素,因为这是Java的,不是Python的。
网站上链接的唯一区别因素是href属性。
所有其他标签都有重复。我不可能百分之百地猜测正确的链接,所以我还需要定位器是由部分文本。
有什么办法开始吗?我可以用其他程序吗?有人成功地做到了吗?我试过找,但没人问这个。

最佳答案

您可以尝试:

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 - Python-Selenium通过href找到元素,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33887410/

10-12 12:56
查看更多