【问题标题】:Select hyperlink in html document using Python and Selenium使用 Python 和 Selenium 在 html 文档中选择超链接
【发布时间】:2015-04-08 15:15:30
【问题描述】:

我正在尝试从网站中选择文档中的超链接,但不确定如何使用 Selenium 选择它。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
names = 'Catostomus discobolus yarrowi'
driver = webdriver.Firefox()
driver.get("http://ecos.fws.gov/ecos/home.action")
SciName = driver.find_element_by_id('searchbox')
SciName.send_keys(names)
SciName.send_keys(Keys.RETURN)

上面的代码到达了我感兴趣的页面,但不确定如何选择超链接。我有兴趣选择第一个超链接。感兴趣的 html 是

<a href="http://ecos.fws.gov/speciesProfile/profile/speciesProfile.action?spcode=E063" data-click="{&quot;p&quot;:1}">Zuni Bluehead Sucker (<strong>Catostomus discobolus</strong> yarrowi)</a>
</h4>
<div class='url'>ecos.fws.gov/speciesProfile/profile/speciesProfile.action?spcode=E063</div>


<span class='description'>
States/US Territories in which the Zuni Bluehead Sucker is known to or is believed to occur: Arizona, New Mexico; US Counties in which the Zuni ...
</span>
<ul class='sitelinks'></ul>
</div>

我猜我可以使用 find_element_by_xpath,但一直无法成功。我希望始终选择第一个超链接。此外,超链接名称将根据输入的物种名称而改变。

【问题讨论】:

    标签: python html selenium


    【解决方案1】:

    我添加了以下代码:

    SciName = driver.find_element_by_css_selector("a[href*='http://ecos.fws.gov/speciesProfile/profile/']")
    SciName.click()
    

    我应该更彻底地阅读 selenium 文档。

    【讨论】:

      【解决方案2】:

      试试这个:

      SciName = driver.find_element_by_link_text("Zuni Bluehead Sucker")
      SciName.click()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-06
        • 2021-12-27
        • 2015-09-30
        • 1970-01-01
        相关资源
        最近更新 更多