Python-Selenium通过href找到元素

aaa*_*n_g 2 html python selenium

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

我试过了

driver.findElement(By.cssSelector("a[href*='text']")).click();
Run Code Online (Sandbox Code Playgroud)

driver.findElement(By.partialLinkText("text")).click();
Run Code Online (Sandbox Code Playgroud)

随着text我正在寻找。这些不适用于错误

AttributeError: 'WebDriver' object has no attribute 'findElement'
Run Code Online (Sandbox Code Playgroud)

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

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

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

反正有开始吗?我可以使用其他程序吗?有人成功做到了吗?我尝试搜索,但没人问过。

Joe*_*ung 6

你可以试试:

from selenium.webdriver.common.by import By
...
driver.find_element(By.PARTIAL_LINK_TEXT, 'text').click()
Run Code Online (Sandbox Code Playgroud)

要么

from selenium import webdriver
...
driver.find_element_by_partial_link_text("text").click()
Run Code Online (Sandbox Code Playgroud)

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

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

资料来源:

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