Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python “面向”;TypeError:类型为'的对象;WebElement';没有len();在执行下面的脚本时_Python_Selenium_Selenium Webdriver - Fatal编程技术网

Python “面向”;TypeError:类型为'的对象;WebElement';没有len();在执行下面的脚本时

Python “面向”;TypeError:类型为'的对象;WebElement';没有len();在执行下面的脚本时,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我正在使用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

我正在使用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))

看起来您使用的是driver.find_element_by_xpath,而不是driver.find_elements_by_xpath。 您可能还希望从中删除空格

"//h2[@class='product-name'] / a"
我认为这不是一个有空格的有效xpath

--编辑- 这是一个有效的xpath,但是切换到通过xpath查找元素对我来说很有效-

driver = webdriver.Chrome(chrome_options = options, executable_path = driver_path)
driver.get('http://magento-demo.lexiconn.com/')
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_elements_by_xpath("//h2[@class='product-name']/a")

print(len(lis))
输出一个9,替换此:

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
仅在找到时返回一个元素

您确定“/h2[@class='product-name']/a”对于您的案例是有效的xpath吗?请使用chrome ConsoleEyes手动检查..这是有效的xpath Only您在chrome console上看到了什么?它捕获了多少个元素?实际上,这个xpath+//h2[@class='product-name']/a”解释了ancor标记“a”捕获站点“”下的12个产品。。我期待着检查这12种产品是否存在……但您是否切换到按类别名称查找元素??这就是我在代码中修复它的原因-我将其编辑回原始答案。仍然面临同样的问题,我没有尝试使用类名。因为这些xpath”//h2[@class='product-name']/a是我手动编写的。实际上,这个xpath”//h2[@class='product-name']/a”解释了ancor标记“a”捕获了站点下的12种产品“magento demo.lexiconn.com/”;…我期待着检查这12种产品是否存在…–vignesh 4分钟前编辑当我尝试使用xpath//h2[@class='product-name']/a时,它在我的末尾显示了9个元素,请重新验证。