测试 selenium 附加到浏览器
This commit is contained in:
parent
ae9739f746
commit
c3c1e6ccc4
19
main.py
19
main.py
@ -1,9 +1,9 @@
|
||||
from selenium import webdriver
|
||||
from selenium.webdriver.support.ui import WebDriverWait
|
||||
import time
|
||||
import run
|
||||
|
||||
import utils
|
||||
|
||||
if __name__ == '__main__':
|
||||
options = webdriver.ChromeOptions()
|
||||
options.add_argument('lang=zh-CN')
|
||||
options.add_argument(
|
||||
@ -14,7 +14,7 @@ options.add_argument('--disable-gpu') # 如果不加这个选项,有时定位
|
||||
|
||||
service = webdriver.ChromeService(r"./driver/chromedriver.exe")
|
||||
driver = webdriver.Chrome(service=service,options=options)
|
||||
driver.maximize_window()
|
||||
# driver.maximize_window()
|
||||
# driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
|
||||
# "source": """
|
||||
# Object.defineProperty(navigator, 'webdriver', {
|
||||
@ -22,17 +22,14 @@ driver.maximize_window()
|
||||
# })
|
||||
# """
|
||||
# })
|
||||
|
||||
try:
|
||||
with open("./stealth.min.js") as f:
|
||||
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
|
||||
"source": f.read()
|
||||
})
|
||||
|
||||
driver.get("https://bot.sannysoft.com/")
|
||||
run.run(driver=driver)
|
||||
time.sleep(5)
|
||||
width = driver.execute_script("return document.documentElement.scrollWidth")
|
||||
height = driver.execute_script("return document.documentElement.scrollHeight")
|
||||
print(width, height)
|
||||
utils.screenshot(driver=driver, save_path="./screenshot.png")
|
||||
driver.set_window_size(width, height)
|
||||
driver.save_screenshot('result.png')
|
||||
time.sleep(30)
|
||||
finally:
|
||||
driver.quit()
|
27
main_subprocess.py
Normal file
27
main_subprocess.py
Normal file
@ -0,0 +1,27 @@
|
||||
import subprocess
|
||||
from selenium import webdriver
|
||||
from selenium.webdriver.chrome.options import Options
|
||||
import time
|
||||
import run
|
||||
import utils
|
||||
|
||||
# 通过 已打开的浏览器 附加 selenium
|
||||
|
||||
if __name__ == '__main__':
|
||||
subprocess.Popen([
|
||||
"C:\Program Files\Google\Chrome\Application\chrome.exe",
|
||||
# "--disable-gpu",
|
||||
"--remote-debugging-port=9222",
|
||||
r'--user-data-dir=E:\Repository\skcks.cn\python-selenium-spider\tmp\UserData'])
|
||||
|
||||
# service = webdriver.ChromeService(r"./driver/chromedriver.exe")
|
||||
chrome_options = Options()
|
||||
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
|
||||
driver = webdriver.Chrome(options=chrome_options)
|
||||
|
||||
try:
|
||||
run.run(driver=driver)
|
||||
time.sleep(5)
|
||||
finally:
|
||||
driver.close()
|
||||
driver.quit()
|
15
run/__init__.py
Normal file
15
run/__init__.py
Normal file
@ -0,0 +1,15 @@
|
||||
from selenium import webdriver
|
||||
from selenium.webdriver.support.ui import WebDriverWait
|
||||
import time
|
||||
from selenium.webdriver.remote.webdriver import WebDriver
|
||||
|
||||
import utils
|
||||
|
||||
|
||||
def run(driver:WebDriver):
|
||||
driver.set_window_size(1920, 1080)
|
||||
driver.get("https://bot.sannysoft.com/")
|
||||
width = driver.execute_script("return document.documentElement.scrollWidth")
|
||||
height = driver.execute_script("return document.documentElement.scrollHeight")
|
||||
print(width, height)
|
||||
utils.screenshot(driver=driver, save_path="./screenshot.png")
|
Loading…
Reference in New Issue
Block a user