selenium启动配置参数接收是ChromeOptions类,创建方式如下:
from selenium import webdriver
option = webdriver.ChromeOptions()
class SELEDEMO:
def __init__(self):
#启动配置参数接收是ChromeOptions类,创建方式如下:
options = webdriver.ChromeOptions()
prefs = {
'profile.default_content_setting_values': {
'images': 2, # 无图
'javascript': 2, # 禁止脚本加载
'permissions.default.stylesheet': 2, # 禁用css
}}
options.add_experimental_option('prefs', prefs)
# 禁用插件
options.add_argument('--disable-plugins')
# 禁用显卡
options.add_argument('--disable--gpu')
# 浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败
options.add_argument("--headless")
# 添加UA
options.add_argument(
"user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/95.0.4638.69 Safari/537.36")
# 指定引擎,可以加快启动时间
self.driver = webdriver.Chrome(service=Service('E:\document\chromedriver-win64\chromedriver.exe'), options=options)
# 刷新页面
self.driver.refresh()
self.wait = WebDriverWait(self.driver, 10)
打开百度——>输入python——>点击搜索——>获取页面中所有的href并打印
编写proxy.py文件,该文件中方法处理代理问题,并将生成的manifest.json和background.js压缩为vimm_chrome_proxyauth_plugin.zip
import string
import zipfile
def create_proxyauth_extension(proxy_host, proxy_port,
proxy_username, proxy_password,
scheme='http', plugin_path=None):
# """Proxy Auth Extension
# args:
# proxy_host (str): domain or ip address, ie proxy.domain.com
# proxy_port (int): port
# proxy_username (str): auth username
# proxy_password (str): auth password
# kwargs:
# scheme (str): proxy scheme, default http
# plugin_path (str): absolute path of the extension
# return str -> plugin_path
# """
if plugin_path is None:
plugin_path = 'vimm_chrome_proxyauth_plugin.zip'
manifest_json = """
{
"version": "1.0.0",
"manifest_version": 2,
"name": "Chrome Proxy",
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"<all_urls>",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"]
},
"minimum_chrome_version":"22.0.0"
}
"""
background_js = string.Template(
"""
var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "${scheme}",
host: "${host}",
port: parseInt(${port})
},
bypassList: ["foobar.com"]
}
};
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
function callbackFn(details) {
return {
authCredentials: {
username: "${username}",
password: "${password}"
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['blocking']
);
"""
).substitute(
host=proxy_host,
port=int(proxy_port),
username=proxy_username,
password=proxy_password,
scheme=scheme,
)
with zipfile.ZipFile(plugin_path, 'w') as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", background_js)
return plugin_path
主要文件
from selenium import webdriver
# 导入选项包
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from time import sleep
from proxy import create_proxyauth_extension
#chromedriver.exe存储位置
s = Service(r"/Users/zhangxia/Desktop/chromedriver")
# 创建chrome参数对象,设置chrome浏览器无界面模式
chrome_options = Options()
# 设置代理
# chrome_options.add_argument("--no-proxy-server")
proxyauth_plugin_path = create_proxyauth_extension("proxy.intra", "80", "name", "psw")
chrome_options.add_extension(proxyauth_plugin_path)
driver = webdriver.Chrome(service=s,options=chrome_options)
driver.get('https://www.baidu.com')
# 输入python
driver.find_element(By.ID,"kw").send_keys("python")
# 点击搜索
driver.find_element(By.ID,"su").click()
# urls = driver.find_elements(By.XPATH,"//a")
try:
# 获取所有的a标签
for link in driver.find_elements(By.XPATH,'//a'):
print(link.get_attribute('href'))
a = link.get_attribute('href')
print(a)
except:
#截图并存储
driver.get_screenshot_as_file("/Users/zhangxia/Desktop/screenshot.png")
finally:
print("==========over=========")
# 关闭
driver.close()