[英]Can someone help me figure out how to print the options in this dropdown?
I've been trying to use selenium so I can list the available size options in this webpage but I am having difficulty identifying the class element.我一直在尝试使用 selenium 以便我可以在此网页中列出可用的尺寸选项,但我很难识别 class 元素。 Every time I use main-size-select-0, I get an error saying unable to locate element.每次我使用 main-size-select-0 时,都会收到一条错误消息,提示无法定位元素。 I have even tried using xpath but it didn't work either.我什至尝试过使用 xpath 但它也不起作用。
Here is my code below:下面是我的代码:
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from bs4 import BeautifulSoup
driver = webdriver.Firefox(executable_path = '/usr/local/bin/geckodriver')
driver.get("https://www.asos.com/nike/nike-air-max-95-logo-leather-trainers-in-dark-navy-orange/prd/20750072?colourwayid=60085113")
select = Select(driver.find_element_by_id("main-size-select-0"));
One of the main problems may be that your code is looking for the menu before it's fully loaded.主要问题之一可能是您的代码在完全加载之前正在寻找菜单。 You can use WebDriverWait
and EC
to hold the code until the element is loaded, then proceed.您可以使用WebDriverWait
和EC
来保存代码,直到加载元素,然后继续。 It doesn't matter too much how you find the element, I used xpath, but you can do whatever you want.如何找到元素并不重要,我使用的是 xpath,但你可以为所欲为。 Once you get it the .text
method returns everything you need.一旦你得到它, .text
方法就会返回你需要的一切。
I split the text on newline and eliminated anything out of stock as well as the first item in the list which is 'Please select'.我在换行符上拆分文本并消除任何缺货以及列表中的第一项“请选择”。
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://www.asos.com/nike/nike-air-max-95-logo-leather-trainers-in-dark-navy-orange/prd/20750072?colourwayid=60085113")
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@id="main-size-select-0"]')))
dd = driver.find_element_by_xpath( '//*[@id="main-size-select-0"]')
[x for x in dd.text.split('\n') if not any(w in x for w in ['Please','Not'])]
Output Output
['UK 6',
'UK 7',
'UK 7.5',
'UK 8',
'UK 8.5',
'UK 9',
'UK 9.5',
'UK 10',
'UK 10.5',
'UK 11',
'UK 12',
'UK 13']
A few things...一些东西...
The items in the list are loaded a bit later than when the page loads.列表中的项目加载时间稍晚于页面加载时间。 I'd place a time.sleep() or WebDriverWait after your driver.get([URL]).我会在你的 driver.get([URL]) 之后放置一个 time.sleep() 或 WebDriverWait。
To get a list of elements in the drop down, you'll want to set your xPath to the "option" tag under "main-size-select-0".要获取下拉列表中的元素列表,您需要将 xPath 设置为“main-size-select-0”下的“option”标签。 For Chrome, that would be: //*[@id="main-size-select-0"]/option Access these using driver.find_element s _by_xpath which would return a list of all the items in the drop down list对于 Chrome,这将是: //*[@id="main-size-select-0"]/option 使用driver.find_element s _by_xpath访问这些将返回下拉列表中所有项目的列表
To summarize...总结...
...
import time
driver = webdriver.Firefox(executable_path = '/usr/local/bin/geckodriver')
driver.get("https://www.asos.com/nike/nike-air-max-95-logo-leather-trainers-in-dark-navy-orange/prd/20750072?colourwayid=60085113")
time.sleep(5)
select_list = Select(driver.find_elements_by_xpath("//*[@id="main-size-select-0"]/option"));
Note that the xPath generated there was done via Chrome.请注意,在那里生成的 xPath 是通过 Chrome 完成的。 It might differ slightly for Firefox. Firefox 可能略有不同。 Just ensure you don't access a particular item in the list ie: //*[@id="main-size-select-0"]/option[0]只要确保您没有访问列表中的特定项目,即: //*[@id="main-size-select-0"]/option[0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.