版权声明:风火编程, 欢迎指正. https://blog.csdn.net/weixin_42620314/article/details/82960165
点击:
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()