【问题标题】:How to get link with special word from webpage using selenium and python?如何使用 selenium 和 python 从网页中获取带有特殊单词的链接?
【发布时间】:2016-02-28 19:32:40
【问题描述】:

我正在编写一个 python 脚本,其目的是从给定的网页中获取包含某个单词的所有链接。

www.example.com/about/someone

www.example.com/profile/someone

www.example.com/about/someone2

因此代码应该只获取那些包含单词about 的链接。 我收到以下错误:

"AttributeError: 'WebElement' object has no attribute 'get' "

这是我的代码:

driver.get("http://example.com/mypage")
time.sleep(20)

links = driver.find_elements_by_tag_name("a")
for link in links:
  if '/about/' in link.get( 'href' ):
     print (link.get_attribute("href"))

【问题讨论】:

  • 使用if '/about/' in link.get_attribute('href')
  • 我收到此错误 TypeError: 'NoneType' 类型的参数不可迭代

标签: python python-3.x selenium


【解决方案1】:
driver.get("http://example.com/mypage")
driver.implicitly_wait(20)

links = driver.find_elements_by_tag_name("a")
for link in links:
  href = link.get_attribute('href')

  if not href:
      continue

  if '/about/' in href:
     print(href)

【讨论】:

    猜你喜欢
    • 2015-05-21
    • 2018-03-26
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多