小编Bri*_*ruz的帖子

'list' 对象在遍历 WebElements 时没有属性 'get_attribute'

我正在尝试使用 Python 和 Selenium 来抓取网页上的多个链接。我正在使用find_elements_by_xpath并且我能够找到一个元素列表,但是我无法更改返回到实际href链接的列表。我知道find_element_by_xpath有效,但这仅适用于一种元素。

这是我的代码:

path_to_chromedriver = 'path to chromedriver location'
browser = webdriver.Chrome(executable_path = path_to_chromedriver)

browser.get("file:///path to html file")

all_trails = []

#finds all elements with the class 'text-truncate trail-name' then 
#retrieve the a element
#this seems to be just giving us the element location but not the 
#actual location

find_href = browser.find_elements_by_xpath('//div[@class="text truncate trail-name"]/a[1]')
all_trails.append(find_href)

print all_trails
Run Code Online (Sandbox Code Playgroud)

此代码返回:

<selenium.webdriver.remote.webelement.WebElement 
(session="dd178d79c66b747696c5d3750ea8cb17", 
element="0.5700549730549636-1663")>, 
<selenium.webdriver.remote.webelement.WebElement 
(session="dd178d79c66b747696c5d3750ea8cb17", 
element="0.5700549730549636-1664")>,
Run Code Online (Sandbox Code Playgroud)

我期待all_trails阵列是像链接列表:www.google.com, www.yahoo.com, www.bing.com。 …

python selenium web-scraping selenium-chromedriver selenium-webdriver

4
推荐指数
1
解决办法
9663
查看次数