-
Notifications
You must be signed in to change notification settings - Fork 90
/
example.py
48 lines (41 loc) · 1.49 KB
/
example.py
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
import base64
from selenium import webdriver
from selenium_stealth import stealth
import math
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
# options.add_argument("--headless")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r"\chromedriver.exe")
stealth(driver,
languages=["en-US", "en"],
vendor="Google Inc.",
platform="Win32",
webgl_vendor="Intel Inc.",
renderer="Intel Iris OpenGL Engine",
fix_hairline=True,
)
print(driver.execute_script("return navigator.userAgent;"))
url = "https://bot.sannysoft.com/"
driver.get(url)
metrics = driver.execute_cdp_cmd('Page.getLayoutMetrics', {})
width = math.ceil(metrics['contentSize']['width'])
height = math.ceil(metrics['contentSize']['height'])
screenOrientation = dict(angle=0, type='portraitPrimary')
driver.execute_cdp_cmd('Emulation.setDeviceMetricsOverride', {
'mobile': False,
'width': width,
'height': height,
'deviceScaleFactor': 1,
'screenOrientation': screenOrientation,
})
clip = dict(x=0, y=0, width=width, height=height, scale=1)
opt = {'format': 'png'}
if clip:
opt['clip'] = clip
result = driver.execute_cdp_cmd('Page.captureScreenshot', opt)
buffer = base64.b64decode(result.get('data', b''))
with open('selenium_chrome_headful_with_stealth.png', 'wb') as f:
f.write(buffer)
driver.quit()