简体   繁体   English

如何解决 TypeError:“WebElement”类型的对象在 Python Selenium 中没有 len()

[英]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上调用。

Solution解决方案

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 .产品名称WebElementoldprice,折扣,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.

相关问题 'WebElement' 类型的 object 没有 len() selenium python - object of type 'WebElement' has no len() selenium python 类型错误:“WebElement”类型的对象没有 len() - TypeError: object of type 'WebElement' has no len() WebElement 类型的 Selenium Object 没有 len() - Selenium Object of Type WebElement has no len() 对于 j in range(len(columns)): TypeError: 'WebElement' 类型的对象没有 len() - for j in range(len(columns)): TypeError: object of type 'WebElement' has no len() TypeError: object of type 'WebElement' has no len() error while looping for all img tags in a web page using selenium webdriver in Python? - TypeError: object of type 'WebElement' has no len() error while looping for all img tags in a web page using selenium webdriver in Python? TypeError:“NoneType”类型的对象在beautifulsoup & selenium Python中没有len() - TypeError: object of type 'NoneType' has no len() in beautifulsoup & selenium Python 如何解决错误:for i in range(len(val)): TypeError: object of type 'NoneType' has no len() ,这可能很傻 - how to resolve Error : for i in range(len(val)): TypeError: object of type 'NoneType' has no len() , it might be silly Python-TypeError:“单元格”类型的对象没有len() - Python - TypeError: object of type 'Cell' has no len() Python - TypeError:类型为'...'的对象没有len() - Python - TypeError: object of type '…' has no len() python - TypeError: object 类型 'zip' 没有 len() - python - TypeError: object of type 'zip' has no len()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM