遍历WebElement时,“列表"对象没有属性"get_attribute" [英] 'list' object has no attribute 'get_attribute' while iterating through WebElements
问题描述
我正在尝试使用Python和Selenium在网页上抓取多个链接.我正在使用find_elements_by_xpath
,并且能够找到元素列表,但是在更改返回到实际href
链接的列表时遇到了麻烦.我知道find_element_by_xpath
可以工作,但是仅适用于一个元素.
I'm trying to use Python and Selenium to scrape multiple links on a web page. I'm using find_elements_by_xpath
and I'm able to locate a list of elements but I'm having trouble changing the list that is returned to the actual href
links. I know find_element_by_xpath
works, but that only works for one element.
这是我的代码:
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
此代码正在返回:
<selenium.webdriver.remote.webelement.WebElement
(session="dd178d79c66b747696c5d3750ea8cb17",
element="0.5700549730549636-1663")>,
<selenium.webdriver.remote.webelement.WebElement
(session="dd178d79c66b747696c5d3750ea8cb17",
element="0.5700549730549636-1664")>,
我希望all_trails
数组是链接的列表,例如:www.google.com, www.yahoo.com, www.bing.com
.
I expect the all_trails
array to be a list of links like: www.google.com, www.yahoo.com, www.bing.com
.
我尝试遍历all_trails
列表并在列表上运行get_attribute('href')
方法,但出现错误:
I've tried looping through the all_trails
list and running the get_attribute('href')
method on the list but I get the error:
有人知道如何将Selenium WebElement转换为href链接吗?
Does anyone have any idea how to convert the selenium WebElement's to href links?
任何帮助将不胜感激:)
Any help would be greatly appreciated :)
推荐答案
让我们看看您的代码中正在发生什么:
Let us see what's happening in your code :
对于有关的HTML
没有任何可见性,似乎以下行在List
find_href
中返回了两个WebElements
,这些反过来又附加到了 List
:
Without any visibility to the concerned HTML
it seems the following line returns two WebElements
in to the List
find_href
which are inturn are appended to the all_trails
List
:
find_href = browser.find_elements_by_xpath('//div[@class="text truncate trail-name"]/a[1]')
因此,当我们打印List
all_trails
时,都将同时打印WebElements
.因此,没有错误.
Hence when we print the List
all_trails
both the WebElements
are printed. Hence No Error.
根据您提供的错误快照,您尝试通过不支持的List
调用 get_attribute("href")
方法.因此,您会看到错误:
As per the error snap shot you have provided, you are trying to invoke get_attribute("href")
method over a List
which is Not Supported. Hence you see the error :
'List' Object has no attribute 'get_attribute'
解决方案:
要获取 href
属性,我们必须遍历List
,如下所示:
Solution :
To get the href
attribute, we have to iterate over the List
as follows :
find_href = browser.find_elements_by_xpath('//your_xpath')
for my_href in find_href:
print(my_href.get_attribute("href"))
这篇关于遍历WebElement时,“列表"对象没有属性"get_attribute"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!