简体   繁体   English

Python-Selenium通过href找到元素

[英]Python - Selenium Locate elements by href

I am trying to find (and click) specific pages for my Python program using selenium. 我正在尝试使用Selenium查找(并单击)我的Python程序的特定页面。

I have tried 我努力了

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

And

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

With text what I am searching for. text我正在寻找。 These do not work with the error 这些不适用于错误

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

I can only assume that it can't do find element because that's for Java, not Python. 我只能假设它找不到元素,因为那是Java而不是Python。

The only distinguishing factor to the links on the website is the href attributes. 网站上链接的唯一区别因素是href属性。

All the other tags have repeats. 所有其他标签都有重复。 There is no way I can 100% guess the right link, so I additionally need the locator to be by partial text. 我无法100%猜出正确的链接,因此我还需要将定位符作为部分文字。

Is there anyway to start this? 反正有开始吗? Could I use other programs? 我可以使用其他程序吗? Has anybody successfully done this? 有人成功做到了吗? I tried searching but nobody has even asked this. 我尝试搜索,但没人问过。

You can try: 你可以试试:

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

or 要么

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

EDIT: For partial text search on the href itself try: 编辑:对于href本身的部分文本搜索,请尝试:

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

Sources: 资料来源:

http://selenium-python.readthedocs.org/faq.html?highlight=click#how-to-auto-save-files-using-custom-firefox-profile 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 http://selenium-python.readthedocs.org/locating-elements.html#locating-elements

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM