如何使用selenium和python从网页上获取特殊单词的链接?

时间:2016-02-28 19:32:40

标签: python python-3.x selenium

我正在制作一个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"))

1 个答案:

答案 0 :(得分:0)

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)