[英]How to resolve TypeError: object of type 'WebElement' has no len() in Python Selenium
I want to print all similar elements but keep getting an error (I am using Pycharm ).我想打印所有类似的元素,但不断收到错误消息(我正在使用Pycharm )。
Error:错误:
TypeError: object of type 'WebElement' has no len()
This line is the one throwing the error: num_page_items = len(productname)
这一行是抛出错误的num_page_items = len(productname)
行: num_page_items = len(productname)
Full selenium code:完整的硒代码:
from selenium import webdriver driver = webdriver.Chrome('/Users/reezalaq/PycharmProjects/untitled2/venv/driver/chromedriver') driver.get("https://www.blibli.com/jual/batik-pria?s=batik+pria") productname = driver.find_element_by_xpath("//div[@class='product-title']") oldprice = driver.find_element_by_css_selector("span.old-price-text").text discount = driver.find_element_by_css_selector("div.discount > span").text saleprice = driver.find_element_by_css_selector("span.new-price-text").text num_page_items = len(productname) for i in range(num_page_items): print(productname[i].text + " : " + oldprice[i].text + " : " + discount[i].text + " : " + saleprice[i].text) driver.close()
You are using find_element_by_xpath
which find and returns the first WebElement
matching the selector.您正在使用find_element_by_xpath
来查找并返回与选择器匹配的第一个WebElement
。 You need to use find_elements_by_xpath
which returns all the matching elements您需要使用find_elements_by_xpath
返回所有匹配的元素
The error says it all :错误说明了一切:
num_page_items = len(productname)
TypeError: object of type 'WebElement' has no len()
productname is assigned the return type from driver.find_element_by_xpath("//div[@class='product-title']")
, which is a WebElement and WebElement have no method as len()
. productname被分配了来自driver.find_element_by_xpath("//div[@class='product-title']")
的返回类型,这是一个WebElement并且WebElement没有len()
方法。 len()
can be invoked on a List
. len()
可以在List
上调用。
As you are trying to access List
items as in :当您尝试访问List
项时:
print(productname[i].text + " : " + oldprice[i].text + " : " + discount[i].text + " : " + saleprice[i].text)
So productname , oldprice , discount and saleprice needs to be of List
type.所以,产品名称,oldprice,折扣和saleprice需要是List
类型。
But your code reads as :但你的代码读作:
productname = driver.find_element_by_xpath("//div[@class='product-title']")
oldprice = driver.find_element_by_css_selector("span.old-price-text").text
discount = driver.find_element_by_css_selector("div.discount > span").text
saleprice = driver.find_element_by_css_selector("span.new-price-text").text
Where productname is a WebElement and oldprice , discount , and saleprice are text .凡产品名称是WebElement和oldprice,折扣,saleprice是文本。 So you need to change them as a List
of WebElements as follows :因此,您需要将它们更改为WebElements List
,如下所示:
productname = driver.find_elements_by_xpath("//div[@class='product-title']")
oldprice = driver.find_elements_by_css_selector("span.old-price-text")
discount = driver.find_elements_by_css_selector("div.discount > span")
saleprice = driver.find_elements_by_css_selector("span.new-price-text")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.