WebDriverWait +下拉大小菜单

时间:2017-04-22 06:46:25

标签: python selenium scripting bots selenium-chromedriver

我正在尝试创建一个自动脚本bot,用于查找特定项目并将项目添加到用户购物车等等。现在我抓住python从下拉菜单中选择大小。

我也使用了WebDriverWait函数,因为它给了我一个未找到元素的错误,所以我认为它是' size'元素尚未加载。

Python也给我这个错误

" TypeError:' str'对象不可调用"

下面是我引用的html代码的图片来提取信息。同时也会欣赏有关更好执行的任何建议。

enter image description here

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

# Open chrome web browswer and directs to supreme.com

browser = webdriver.Chrome()
browser.get('http://www.supremenewyork.com/shop/all')

#Find specific item 

browser.find_element_by_xpath('//*
[@id="container"]/article[14]/div/a/img').click()

#Wait for element to load 

pause = WebDriverWait(browser,10).until(
EC.visibility_of_any_elements_located(By.ID('size'))
)


# Select size 

Select = Select(browser.find_element_by_id('size'))
Select.select_by_visible_text("Large")

1 个答案:

答案 0 :(得分:1)

尝试以下代码。

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("Medium")