为什么zip()函数不能使用Python/Selenium在列表中返回预期结果?
我有一个使用Python/Selenium的代码块,该代码块应该遍历网页上的各种项目,并显示该项目是否可用,如果可用,则显示该项目的名称和颜色,以及提供指向该页面的链接。代码在售罄的项目上按预期工作,但当它到达第一个可用项目时,python只返回“available”,并提供页面上最后一个项目的名称/url,而不是预期的项目。代码块:为什么zip()函数不能使用Python/Selenium在列表中返回预期结果?,python,list,selenium,web-scraping,zip,Python,List,Selenium,Web Scraping,Zip,我有一个使用Python/Selenium的代码块,该代码块应该遍历网页上的各种项目,并显示该项目是否可用,如果可用,则显示该项目的名称和颜色,以及提供指向该页面的链接。代码在售罄的项目上按预期工作,但当它到达第一个可用项目时,python只返回“available”,并提供页面上最后一个项目的名称/url,而不是预期的项目。代码块: shirts = driver.find_elements_by_xpath("""//*[@id='container']/article/div/h1/a""
shirts = driver.find_elements_by_xpath("""//*[@id='container']/article/div/h1/a""")
colors = driver.find_elements_by_xpath("""//*[@id='container']/article/div/p/a""")
articles = driver.find_elements_by_tag_name('article')
for article in articles:
ActionChains(driver).move_to_element(article).perform()
if article.find_element_by_tag_name('a').text == "sold out":
print("sold out")
elif article.find_element_by_tag_name('a').text == "":
ActionChains(driver).move_to_element(article).perform()
print("available")
for shirt, color in zip(shirts, colors):
shirt_text = shirt.text
color_text = color.text
print shirt_text, color_text
link = article.find_element_by_xpath('div/a').get_attribute('href')
print(link)
下面是上述代码返回的代码片段:
(应显示的预期项目为品红色广场太阳镜)
还有我正在抓取的页面链接:
是我的脚本设置不正确,还是我完全遗漏了什么?它只打印最后一个产品名称,因为您的
衬衫列表包含整个页面上的所有产品名称,并且衬衫文本
设置为该列表中的最后一项
相反,将对名称和颜色的查询移动到文章循环中:
for article in articles:
if article.find_element_by_tag_name('a').text == 'sold out':
print('sold out')
else:
print('available')
[...]
nameLink = article.find_element_by_xpath('div/h1/a')
colorLink = article.find_element_by_xpath('div/p/a')
[...]
将查询移动到已售完商品的顶部文章循环或底部循环?只有当商品可用时,才能提取名称。
for article in articles:
if article.find_element_by_tag_name('a').text == 'sold out':
print('sold out')
else:
print('available')
[...]
nameLink = article.find_element_by_xpath('div/h1/a')
colorLink = article.find_element_by_xpath('div/p/a')
[...]