D28 Selenium 测试网页与参数

昨天用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

<<:  [D19] DL 深度学习(2)

>>:  建立 React component

[Day 3] 前後端技能这麽多,要选哪个呢?

本来打算一篇写完的,结果居然要分成三篇 XD 前端工具挑选 前端的部分可以搭配框架来建立 比较有名的...

[Angular] Day16. Writing structural directives

在上一章中介绍了如何建立客制化的 attribute directive 与使用,而本章将会介绍如何...

Ruby、演算法学习心得(一) 二元搜寻法 Binary Search。

铁人赛结束後一阵空虚?? 文章内容都会以Ruby来撰写程序码,然後继续来传教K-POP啦! 有请韩国...

python入门学习day 4

主题: 变量类型 & 进位制 在Python语言中,我们可以使用变量来保存数据,变量有不同的...