微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在python中使用selenium获取所有href链接

我在python中练习selenium,我想使用selenium获取网页上的所有链接.

例如,我想要来自此网站的“a href”标签中的所有链接http://psychoticelites.com/

我写了一个脚本,它正在运行.但是,它给了我对象地址.我尝试使用’id’标记获取值,但是,它不起作用.

我目前的剧本: –

from selenium import webdriver
from selenium.webdriver.common.keys import Keys



driver = webdriver.Firefox()
driver.get("http://psychoticelites.com/")
assert "Psychotic" in driver.title
continue_link = driver.find_element_by_tag_name('a')
elem = driver.find_elements_by_xpath("//*[@href]")
#x = str(continue_link)
#print continue_link
#print elem
z = elem
print z

任何类型的线索/提示将不胜感激.

解决方法:

好吧,你必须简单地遍历列表.

elems = driver.find_elements_by_xpath("//a[@href]")
for elem in elems:
    print elem.get_attribute("href")

find_elements_by_ *返回元素列表(注意’elements’的拼写).循环遍历列表,获取每个元素并从中获取所需的属性值. (在这种情况下是href)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐