python - 尝试使用 selenium python 获取下拉菜单的长度时元素不存在错误
问题描述
def addtocart():
driver.get("https://www.mrporter.com/en-gb/mens/product/nike/shoes/low-top-sneakers/space-hippie-04-recycled-stretch-knit-sneakers/19971654707345242")
#driver.get("https://www.mrporter.com/en-gb/mens/product/nike/shoes/low-top-sneakers/plus-sacai-blazer-low-colour-block-leather-sneakers/10163292708776845?ntfyeu=jo5suw")
txt = driver.find_element_by_xpath("/html/body/main/div/div[2]/div/div[1]/div[2]/div[8]/div[2]").text
while "Sorry, this item is sold out" in txt:
txt = driver.find_element_by_xpath("/html/body/main/div/div[2]/div/div[1]/div[2]/div[8]/div[2]").text
time.sleep(2)
driver.refresh()
print("Item out of stock , waiting for product")
else:
print("The product is in stock!")
#Locates dropdown menu and clicks it
dropdown = driver.find_element_by_xpath("/html/body/main/div/div[2]/div/div[1]/div[2]/div[6]/div/div/div")
dropdown.click()
select=Select(driver.find_element_by_class_name("CombinedSelect11__field CombinedSelect11__field--placeholderText CombinedSelect11__field--nativeSelect"))
print(len(select.options))
添加到购物车()
我想
- 找到下拉菜单的选择类 2)获取下拉菜单的长度 3)选择一个介于 0 和下拉菜单长度之间的随机数 4)单击与该数字匹配的选项。
它说该元素不存在。我尝试使用完整的 x-path 和类名,但是都给了我同样的错误。
解决方案
试试这个选择器,而不是完整的 xpath。我可以单击并打开下拉列表。
dropdown = driver.find_element_by_css_selector('div.CombinedSelect11')
也代替
select=Select(driver.find_element_by_class_name("CombinedSelect11__field CombinedSelect11__field--placeholderText CombinedSelect11__field--nativeSelect"))
尝试
select=Select(driver.find_element_by_css_selector("CombinedSelect11__field.CombinedSelect11__field--placeholderText.CombinedSelect11__field--nativeSelect"))
class_name 函数不处理类名之间的空格。
推荐阅读
- dialogflow-es - 如何正确分析DialogFlow中的“明天早上9点看书”之类的词?
- static - 网站元未显示在谷歌搜索中
- java - Java.util.Date 表示时区不知道在不同时区工作正常
- php - 从多维数组 PHP 中选择唯一的组合
- jquery - TableSorter:在列上随机排序
- python - “ipywidgets 下拉小部件:什么是 onchange 事件?”的基本问题
- react-pdf - 包含 react-pdf 渲染器时的开玩笑错误
- javascript - 让一个项目有条件地出现在另一个组件中
- javascript - 如何在 Node.js/Javascript 中添加新的键值对
- kernel - KMDF windows 驱动程序中的 ibv_post_send 性能优化