简体   繁体   中英

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. Every time I use main-size-select-0, I get an error saying unable to locate element. I have even tried using xpath but it didn't work either.

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. It doesn't matter too much how you find the element, I used xpath, but you can do whatever you want. Once you get it the .text method returns everything you need.

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

['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...

  1. 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]).

  2. 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". 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

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. It might differ slightly for Firefox. Just ensure you don't access a particular item in the list ie: //*[@id="main-size-select-0"]/option[0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM