1

我在 Python 中使用 Splinter 库。我希望根据链接文本单击链接,但这不起作用。

该链接包含在源中,例如:

<a href="link here"><span style="color:#000000;">link text here</span></a>

使用“ browser.click_link_by_text('link text here')”会报错:

splinter.exceptions.ElementDoesNotExist:找不到带有文本链接“此处链接文本”的元素

4

4 回答 4

6

我也曾click_link_by_text多次偶然发现函数,但成功完成了以下其余部分。您可以尝试其中任何一种。

browser.click_link_by_href('link here')          # From "link here"
browser.click_link_by_partial_href('link')       # From "link here"
browser.click_link_by_partial_text('link text')  # From "link text here"

您可以在此处关注文档: https ://splinter.readthedocs.io/en/latest/api/driver-and-element-api.html#splinter.driver.DriverAPI.click_link_by_text

于 2017-09-13T13:32:17.663 回答
0

你能提供完整的代码吗?我不能说为什么它不适合你,因为它对我来说很好用下面的代码。

 from splinter import Browser

 browser = Browser('chrome')
 browser.visit("https://www.google.co.in/")
 browser.fill('q','facebook')
 button = browser.find_by_name('btnG')
 button.click()
 browser.click_link_by_text("Facebook Login")
于 2015-05-27T17:46:01.667 回答
0

试试看~~~~

from splinter import Browser
browser = Browser('chrome')
browser.windows.current = browser.windows[0] #maybe you have a lot of windows.
xpath = '//div[@class="big-play-button"]'
browser.find_by_xpath(xpath).click()
于 2016-01-15T09:46:50.787 回答
0

One option would be to use developer tools or Firebug to find the xpath of the element you are interested in and then use find_by_xpath based on that

from splinter import Browser

with Browser() as browser:
    browser.visit("http://www.thisamericanlife.org")
    browser.fill('keys', 'relationships')
    button = browser.find_by_xpath('/html/body/div[2]/div[2]/div[1]/div[2]/div/form/div/input[1]').click()
    print browser.url

This fills in the form (in this case for the search term "relationships") and then navigates to the page http://www.thisamericanlife.org/search?keys=relationships

于 2016-06-20T05:40:13.140 回答