selenium使用技巧

@高效码农  July 5, 2019

一、selenium不弹出浏览器,后台静默运行

from selenium.webdriver.chrome.options import Options
#打开浏览器
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
# 驱动路径
path = 'F:/python_workspace/customs/chromedriver.exe'
# 创建浏览器对象
driver = webdriver.Chrome(executable_path=path, chrome_options=chrome_options)
driver.get('https://www.baidu.com')
注意:
必须导入:from selenium.webdriver.chrome.options import Options

二、selenium打开标签页,并获取句柄

js = r'window.open("www.baidu.com");'
driver.execute_script(js)
handles = driver.window_handles
driver.switch_to_window(handles[n+1])
n = n + 1


添加新评论