'list' 对象在遍历 WebElements 时没有属性 'get_attribute' [英] 'list' object has no attribute 'get_attribute' while iterating through WebElements
问题描述
我正在尝试使用 Python 和 Selenium 来抓取网页上的多个链接.我正在使用 find_elements_by_xpath
并且我能够找到一个元素列表,但是我无法更改返回到实际 href
链接的列表.我知道 find_element_by_xpath
有效,但这只对一个元素有效.
这是我的代码:
path_to_chromedriver = 'chromedriver 位置的路径'浏览器 = webdriver.Chrome(executable_path = path_to_chromedriver)browser.get("file:///html 文件的路径")all_trails = []#找到所有具有text-truncate trail-name"类的元素,然后#获取a元素#这似乎只是给了我们元素位置而不是#实际位置find_href = browser.find_elements_by_xpath('//div[@class="text truncate trail-name"]/a[1]')all_trails.append(find_href)打印 all_trails
此代码正在返回:
,<selenium.webdriver.remote.webelement.WebElement(会话=dd178d79c66b747696c5d3750ea8cb17",元素="0.5700549730549636-1664")>,
我希望 all_trails
数组是一个链接列表,例如:www.google.com、www.yahoo.com、www.bing.com
.>
我尝试遍历 all_trails
列表并在列表上运行 get_attribute('href')
方法,但出现错误:
有人知道如何将 selenium WebElement 转换为 href 链接吗?
任何帮助将不胜感激:)
让我们看看您的代码发生了什么:
对相关 HTML
没有任何可见性,似乎以下行将两个 WebElements
返回到 List
find_href
依次附加到 all_trails
List
:
find_href = browser.find_elements_by_xpath('//div[@class="text truncate trail-name"]/a[1]')
因此,当我们打印 List
all_trails
时,WebElements
都会被打印出来.因此没有错误.
根据您提供的错误快照,您正在尝试通过 List
调用 get_attribute("href")
方法不支持.因此你会看到错误:
'List' 对象没有属性 'get_attribute'
解决方案:
要获得 href
属性,我们必须遍历 List
如下:
find_href = browser.find_elements_by_xpath('//your_xpath')对于 find_href 中的 my_href:打印(my_href.get_attribute(href"))
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.
Here is my code:
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
This code is returning:
<selenium.webdriver.remote.webelement.WebElement
(session="dd178d79c66b747696c5d3750ea8cb17",
element="0.5700549730549636-1663")>,
<selenium.webdriver.remote.webelement.WebElement
(session="dd178d79c66b747696c5d3750ea8cb17",
element="0.5700549730549636-1664")>,
I expect the all_trails
array to be a list of links like: www.google.com, www.yahoo.com, www.bing.com
.
I've tried looping through the all_trails
list and running the get_attribute('href')
method on the list but I get the error:
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 :
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]')
Hence when we print the List
all_trails
both the WebElements
are printed. Hence No Error.
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'
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"))
这篇关于'list' 对象在遍历 WebElements 时没有属性 'get_attribute'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!