Splinter - 如何单击作为 div 或 span 元素实现的 link / 按钮

Splinter - How to click a link / button implemented as a div or span element

我在 Python 中使用 Splinter 库。我想根据 link 文本单击 link,但这不起作用。

link 包含在源代码中,例如:

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

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

splinter.exceptions.ElementDoesNotExist: no elements could be found with link by text "link text here"

能否提供完整的代码?我不能说为什么它对你不起作用,因为它对我来说用下面的代码工作得很好。

 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")

试试看~~~~

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()

一种选择是使用开发人员工具或Firebug找到您感兴趣的元素的 xpath,然后基于此

使用find_by_xpath
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

填写表格(在本例中为搜索词 "relationships"),然后导航至页面 http://www.thisamericanlife.org/search?keys=relationships

我也曾多次偶然发现 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