昨天用Selenium IDE 測試網頁但是一值找不到方法設隨機變數
今天找了python套件selenium來用看看
首先安裝
pip install selenium
然後要去下載跟你的瀏覽器相同版本的chrome driver來用
https://chromedriver.chromium.org/downloads
以下是開啟一個簡單測試網站會讀取你瀏覽器相關的數據
還有其他好用的chrome_options參數
test/tests.py
import json
from pathlib import Path
from time import sleep
import os
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
file_path = Path(__file__).parent
chrome_options = webdriver.ChromeOptions()
capabilities = DesiredCapabilities.CHROME
capabilities["goog:loggingPrefs"] = {"performance": "ALL"} # chromedriver 75+
# chrome_options.add_argument('--disable-gpu') # 規避google bug
# chrome_options.add_argument('--headless') # 無頭模式
# chrome_options.add_argument('blink-settings=imagesEnabled=false') # 不載入圖
# chrome_options.add_argument("--incognito") # 使用無痕模式
# proxy = "socks5://localhost:9050"
# chrome_options.add_argument('--proxy-server={}'.format(proxy)) # 讓 selenium透過 tor訪問 internet
# ua = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0"
# chrome_options.add_argument("user-agent={}".format(ua))
chrome_path = f'{file_path}/driver/chromedriver.exe'
driver = webdriver.Chrome(
executable_path=chrome_path,
chrome_options=chrome_options,
desired_capabilities=capabilities,
)
driver.maximize_window()
driver.get('http://gs.statcounter.com/detect')
cookie_list = driver.get_cookies() # 設定cookies
logs = driver.get_log("performance")
driver.refresh() # 重新載入
# driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") # 滾輪滑動
# driver.execute_script("window.scrollBy(-330,-330);")
# soup = BeautifulSoup(driver.page_source, 'html.parser')
# urls = soup.find_al;l('a',href=re.compile("/p/B"))
# driver.close() # 關閉driver