«
python web自动化 selenium 开局代码
myluzh 发布于
阅读:554
Python
chromedriver()函数,自动安装驱动
import random
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
def chromedriver():
chromedriver_path = ChromeDriverManager().install()
return chromedriver_path
def main():
option = webdriver.ChromeOptions()
option.add_argument('--disable-gpu')
# option.add_argument('--incognito') # 无痕模式
# option.add_argument('--headless') # 无头模式
option.add_argument('window-size=320x627')
option.add_argument('--start-maximized')
option.add_argument('--ignore-certificate-errors') # 忽略证书错误
s = Service(chromedriver())
browser = webdriver.Chrome(options=option, service=s)
browser.implicitly_wait(10)
browser.get('https://www.baidu.com')
time.sleep(1)
# browser.find_element(By.XPATH, "//[@id='input_username']").send_keys("1")
# browser.find_element(By.XPATH, "//*[@id=\"btn_login\"]").click()
if __name__ == "__main__":
main()
python selenium