風火程式設計--selenium使用
阿新 • • 發佈:2018-11-19
點選:
https://blog.csdn.net/jojoy_tester/article/details/53453888
等待:
chromedriver的一些初始化設定
from selenium.webdriver.chrome.options import Options chrome_options = Options() # 設定無頭 chrome_options.add_argument('--headless') # 設定不載入圖片 prefs = {"profile.managed_default_content_settings.images": 2} chrome_options.add_experimental_option("prefs", prefs) driver = webdriver.Chrome( chrome_options=chrome_options, executable_path=r'D:\software\python36\chromedriver.exe')
等待
顯式等待
from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait target = wait.until(EC.element_to_be_clickable((By.XPATH, '//div[@class="reader-download btn-download"]'))) target.click()
隱式等待
wait = WebDriverWait(driver, max_wait)
driver.implicitly_wait(seconds)
強制等待
time.sleep(seconds)
新標籤中開啟url, 並切換到該標籤
js = 'window.open("{}");'.format(dl_url)
driver.execute_script(js)
driver.switch_to_window(driver.window_handles[1])
視窗最大化
最大化方式開啟
chrome_options = Options() chrome_options.add_argument('--start-maximized')
已開視窗最大化
driver.maximize_window()
iframe的處理
sleep(2)
iframe = driver.find_element_by_xpath('//iframe[@class="iframe"]')
driver.switch_to.frame(iframe)
driver.switch_to_default_content()
load = driver.find_element_by_xpath('//a[@id="WkDialogOk"]')
load.click()
driver.close()