Linux+python+selenium+自动截图

某些原因需要自动截图。。

参考文档:

操作cookie:https://blog.csdn.net/ytraister/article/details/106033630

参数: https://blog.csdn.net/weixin_43968923/article/details/87899762

https://blog.csdn.net/m0_50944918/article/details/113185800

鼠标点击: http://www.360doc.com/content/22/1112/08/65839882_1055570473.shtml

模拟手机:https://blog.csdn.net/xiao_yi_xiao/article/details/122220828

安装环境: https://blog.csdn.net/zxp3817100/article/details/125089490

Linux系统需要安装中文字体支持

获取cookie可以先不使用–headless

1
2
3
time.sleep(200)
print(json.dumps(driver.get_cookies())) # 有多个的话会是个list,后面add_token需要一个一个加
driver.close()

转换cookie时如果里面有中文不要转义ascii

1
json.dumps(login_ret, ensure_ascii=False)

最终代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*-
import json
import urllib.parse

import requests
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 selenium.webdriver.common.action_chains import ActionChains
import time


if __name__ == '__main__':
options = Options()
options.add_argument('--no-sandbox')
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--hide-scrollbars')
options.add_argument('lang=zh_CN.UTF-8')
# 设置这两个参数就可以避免密码提示框的弹出
prefs = {"credentials_enable_service": False, "profile.password_manager_enabled": False}
options.add_experimental_option("prefs", prefs)
options.add_experimental_option('excludeSwitches', ['enable-automation']) # 规避检测
options.add_argument(
'user-agent=Mozilla/5.0 (Linux; U; Android 11; zh-CN; GM1910 Build/RKQ1.201022.002) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/69.0.3497.100 UWS/3.22.1.233 Mobile Safari/537.36 AliApp(DingTalk/6.5.45) com.alibaba.android.rimet/26284409 Channel/263200 language/zh-CN abi/64 UT4Aplus/0.2.25 colorScheme/light') # 模拟手机
driver = webdriver.Chrome(service=Service('D:\ChromeData\chromedriver.exe'), options=options) # 驱动地址
driver.set_window_size(width=412, height=915)
# driver.maximize_window()
driver.get('http://xxxxxx')
driver.delete_all_cookies() # 添加cookie前清除所有cookie
driver.add_cookie(
{"domain": "xxx", "httpOnly": False, "name": "auth_user", "path": "/", "secure": False,
"value": encode_login_ret})
driver.add_cookie({"domain": "xxx", "httpOnly": True, "name": "JSESSIONID", "path": "/xxxx",
"secure": False, "value": '你的cookie'})
# driver.refresh()
driver.get('http://xxxx') # 这一步进入需要cookie的页面
time.sleep(2)

# 获取总页面大小,以防截图元素不全,我是模拟手机,所以不需要
# width = driver.execute_script("return document.documentElement.scrollWidth")
# height = driver.execute_script("return document.documentElement.scrollHeight")
# driver.set_window_size(width=width, height=height)

# 鼠标点击
# el = driver.find_element(By.XPATH, '/html/body/div[9]/div/div[4]/div[3]/a') # 这里在chrome devtool 里选择元素右键就可复制XPATH
# ActionChains(driver).move_to_element(el).click().perform()
time.sleep(3)
driver.get_screenshot_as_file('./tmp.png')
driver.close()
# print(json.dumps(driver.get_cookies()))