Win10整合selenium使用google-chrome的headless

第一部分:安裝chrome

Google Chrome 80.0.3987.163

第二部分:配置自動化環境與運行

1.下載chromedriver

http://npm.taobao.org/mirrors/chromedriver/
wget http://npm.taobao.org/mirrors/chromedriver/80.0.3987.106/chromedriver_linux64.zip

找到與google-chrome對應版本

第三部分: 裝selenium(自行安裝)

pip install selenium

第四部分 實例1:

browser-test.py

import sys
import json
import urllib.parse

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait


def browserExcute(browser,url,time,fileId):
    try:
        browser.get(url)
        wait = WebDriverWait(browser,time)
        print(brower.page_source)
        #writeSourceToFile(browser.page_source,fileId)
    finally:
        browser.close()

def initWebDriver():
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('--disable-dev-shm-usage')

    return webdriver.Chrome(executable_path='E:/LServer/chromedriver/chromedriver_win32/chromedriver.exe', chrome_options=chrome_options)

if __name__ == "__main__":
    url = "http://npm.taobao.org/mirrors/chromedriver/80.0.3987.16/"
    time = 10
    fileId = '11111111111'

    brower = initWebDriver()

    browserExcute(brower,url,time,fileId)

運行 python browser-test.py

<html><head>
    <meta charset="utf-8">
    <title>ChromeDriver Mirror</title>

注意

1.將C:\Program Files (x86)\Google\Chrome\Application添加到Path環境變量中去
2.executable_path要指定正確

完成。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章