我正在用 Python 练习 Selenium,我想使用 Selenium 获取网页上的所有链接。
例如,我想要 href=
中的所有链接所有 <a>
的属性(property)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)
最佳答案
嗯,你必须简单地遍历列表:
elems = driver.find_elements_by_xpath("//a[@href]")
for elem in elems:
print(elem.get_attribute("href"))
find_elements_by_*
返回一个元素列表(注意“elements”的拼写)。遍历列表,获取每个元素并从中获取所需的属性值(在本例中为 href
)。
关于python - 在python中使用selenium获取所有href链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34759787/