Selenium WebDriver- 使用显示等待,判断搜狗的输入框是否显示,按钮是否可点击,然后在输入光荣之路搜索词,然后在点击搜索。
#encoding=utf-8 from selenium import webdriver import time from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC def visibility(webelement): wait = WebDriverWait(driver, 10, 0.2) wait.until(EC.visibility_of(webelement)) def clickable(by,locator_path): wait = WebDriverWait(driver, 10, 0.2) element = wait.until(EC.element_to_be_clickable((By.ID,"stb"))) return element driver = webdriver.Ie(executable_path = "e:\\IEDriverServer") driver.get("http://www.sogou.com") input_box=driver.find_element_by_id("query") visibility(input_box) input_box.send_keys(u"光荣之路") button = clickable(By.ID,"stb") button.click() time.sleep(3) driver.close()