python - 执行以下脚本时遇到“TypeError:'WebElement' 类型的对象没有 len()”
问题描述
我正在尝试使用 assert equal 函数检查产品是否为 12。请检查我尝试过的以下脚本:
def test_search(self):
driver=self.driver
driver.get("http://magento-demo.lexiconn.com/")
driver.maximize_window()
driver.find_element_by_xpath(".//*[@id='search']").send_keys("Bed & Bath")
driver.find_element_by_xpath(".//*[@id='search_mini_form']/div[1]/button").click()
lis = driver.find_element_by_xpath("//h2[@class='product-name'] / a")
self.assertEqual(12,len(lis))
解决方案
替换这个:
lis = driver.find_element_by_xpath("//h2[@class='product-name']/a")
至 :
lis = driver.find_elements_by_xpath("//h2[@class='product-name']/a")
请注意,这find_elements
将返回一个web 元素列表,如果找到, as将只返回一个元素。 find_element
推荐阅读
- javascript - JS RemoveEventListener 按类名来自动态添加的元素
- spring-boot - 声明列 TIME WITH TIME ZONE 时出现语法错误
- r - 以周计算时差
- javascript - 我想在我的表中插入一个按钮,当用户在搜索字段中搜索某些内容时使用 javascript 显示费用
- python - 如何修复错误以将字符串转换为 python 中的浮点数?
- javascript - Promise.prototype.catch()的回调函数中是否需要包含参数?
- c - gcc/clang:错误:冲突的类型
: 为什么函数名很重要? - spring-boot - Spring boot 2.5 不使用 MySql 复制驱动程序
- java - 将共享方法移动到 Java 中的 Util 类?
- ubuntu - 如何解决ubuntu ZYNADDSUBFX软件中“默认IO没有初始化。默认为NULL后端”?