Python-Selenium通过h定位元素

2024-10-03 00:27:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用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属性。

所有其他标签都有重复。我不可能百分之百地猜测正确的链接,所以我还需要定位器是由部分文本。

有什么办法开始吗?我可以用其他程序吗?有人成功地做到了吗?我试过找,但没人问这个。


Tags: text程序by链接driverselenium错误页面
1条回答
网友
1楼 · 发布于 2024-10-03 00:27:46

您可以尝试:

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

相关问题 更多 >