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
import time
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://motul.lubricantadvisor.com/Default.aspx?data=1&lang=ENG&lang=eng")
def getallcars():
wait = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((By.ID, "ctl00_ContentPlaceHolder1_rptCategoryBtn_ctl01_btnImage")))
driver.find_element(By.ID, "ctl00_ContentPlaceHolder1_rptCategoryBtn_ctl01_btnImage").click()
wait.until(EC.presence_of_element_located((By.ID, "ctl00_ContentPlaceHolder1_lblSelectedMake")))
driver.find_element(By.ID, 'ctl00_ContentPlaceHolder1_lblSelectedMake').click()
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#ctl00_ContentPlaceHolder1_lstMake")))
el = driver.find_element(By.NAME,"ctl00$ContentPlaceHolder1$lstMake")
car =[]
for option in el.find_elements(By.TAG_NAME,'option'):
car.append((option.text).encode('utf8'))
return car
cars=getallcars()
for value in cars:
drop = driver.find_element(By.CSS_SELECTOR, '#ctl00_ContentPlaceHolder1_lstMake')
sel = Select(drop)
sel.select_by_visible_text(value)
time.sleep(2) #<---- THIS POINT!!
driver.find_element(By.ID,'ctl00_ContentPlaceHolder1_HeaderModel').click()
el2 = driver.find_element(By.NAME, "ctl00$ContentPlaceHolder1$lstModel")
print "The models for %s are:" %value
for option in el2.find_elements(By.TAG_NAME,'option'):
print option.text
action = ActionChains(driver)
action.move_to_element_with_offset(el2, 300, 200)
action.click()
action.perform()
driver.find_element(By.CSS_SELECTOR,'#ctl00_ContentPlaceHolder1_HeaderMake').click()
我一直在做爬虫。我完全不明白。所以我有一个问题。也许这是代码的34行。我是关于#的标志
它是"time.sleep(2)"
方法的用法。因为当它改变“sel.select_by_visible_text(value)
”
我该怎么做?我不想使用“time.sleep(2)
”方法。
我已经试过“expected_conditions.presence_of_element_located
”它不起作用。我猜这是关于Dropbox的问题。这个尺寸基本上不是因为当我尝试了另一个尺寸的“expected_conditions.presence_of_element_located
”
答案 0 :(得分:1)
显式等待不起作用,因为您可以使用的条件是“可点击的元素”,“要显示的元素”等。 您用于显式等待的元素是可用的,也可以单击,但它失败,因为其他元素与它重叠。
由于其他元素的重叠需要时间消失,我们必须等待重叠消失才能点击元素。 显式等待可以等待元素出现和可点击,它已经存在但是被其他元素隐藏了。
在这种情况下,我们必须使用time.sleep()来进行艰难的等待