Reputation: 73
I am trying trying to make an auto checkout script but i am stuck with selecting a specific size from a dropdown list
from selenium import webdriver
import requests
driver = webdriver.Chrome()
driver.get('http://www.supremenewyork.com/shop/all')
driver.find_element_by_xpath('//*[@id="container"]/article[112]/div/a').click()
driver.find_element_by_xpath('//*[@id="size"]/option[2]').click()
Below is the html of the dropdown size selection and I am copying the xpath but still can't locate element , why ?
Upvotes: 0
Views: 567
Reputation: 967
Try below code.
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
browser.get('http://www.supremenewyork.com/shop/all')
browser.find_element_by_xpath('//*[@id="container"]/article[12]/div/a/img').click()
WebDriverWait(browser,10).until(EC.visibility_of_any_elements_located((By.ID,'size')))
select = Select(browser.find_element_by_id('size'))
select.select_by_visible_text("Large")
Upvotes: 0
Reputation: 302
For drop downs you cannot use driver.findElement directly. You should be using the Select api.
In Java,
Select sel = new Select (driver.findElement (By.name ("size"))
sel.selectByvalue or index or visible text
can be used. You can refract ur code to python.. above is java code.
Upvotes: 0
Reputation: 25596
Take a look at the Select
class. You should be using it any time you are dealing with a SELECT element ... it will make your life much easier.
Your code should look like
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_id('size'))
select.select_by_visible_text("Medium")
Upvotes: 1