中介网站建设,引擎搜索入口,网站建设公司合肥,如何创建网站赚钱python如何优雅的重启谷歌游览器#xff1f;
代码很简单#xff1a;
import subprocesshomepage about:blank
# 结束已经启动的谷歌游览器
subprocess.run(taskkill /f /im chrome.exe, shellTrue)
# debug启动谷歌游览器
subprocess.run([…python如何优雅的重启谷歌游览器
代码很简单
import subprocesshomepage about:blank
# 结束已经启动的谷歌游览器
subprocess.run(taskkill /f /im chrome.exe, shellTrue)
# debug启动谷歌游览器
subprocess.run([start, chrome, homepage,--remote-debugging-port9222], shellTrue)注意shellTrue是必须的因为start是命令行的命令。 为什么start命令可以直接启动谷歌
经过测试在命令行中执行chrome提示找不到但执行start chrome却能够顺利的启动
chrome
chrome 不是内部或外部命令也不是可运行的程序或批处理文件。
start chrome在Windows中直接执行会在当前目录和环境变量中查找但是start启动的机制却有所区别start不仅会在当前目录和环境变量中查找命令还会在注册表HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\和HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\
中查找命令。
之所以start chrome可以直接启动谷歌就是因为注册表\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe的存在我们也可以通过读取该注册表获取谷歌游览器的位置
import winregkey winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe)
chrome_path winreg.QueryValue(key, )
chrome_pathC:\\Program Files\\Google\\Chrome\\Application\\chrome.exe还可以获取谷歌游览器的两个位置是\HKEY_CLASSES_ROOT\Applications\chrome.exe\shell\open\command的值和HKEY_CLASSES_ROOT\ChromeHTML\Application的ApplicationIcon项但它们需要对字符串进行额外处理。
如何优雅的过cf
方案1 重启谷歌游览器后等待一段时间后再自动化工具接管游览器
from selenium import webdriver
import subprocess
# from selenium.webdriver.support import expected_conditions as EC
# from selenium.webdriver.support.ui import WebDriverWait
# from selenium.webdriver.common.by import By
import timeurl https://cf地址/
# 结束已经启动的谷歌游览器
subprocess.run(taskkill /f /im chrome.exe, shellTrue)
# debug启动谷歌
subprocess.run([start, chrome, url,--remote-debugging-port9222], shellTrue)
# 等待cf通过
time.sleep(10)
# 接管已经经过cf验证的游览器
option webdriver.ChromeOptions()
option.add_experimental_option(debuggerAddress, 127.0.0.1:9222)
browser webdriver.Chrome(optionsoption)
browser实际等待时间根据网页实际情况来。cf往往有一个5秒盾的概念所以等待6秒以上是合适的然后根据网站本身的延迟增加时间。
如果你觉得10秒等待太久希望手动告诉游览器cf已经通过可以查看方案2。
方案2 代码启动谷歌游览器后等待谷歌游览器关闭再重新启动谷歌并接管。
对于cf站只要经过检测后往往都有缓存不会再次进入检测所以方案2有效但是需要先获取谷歌游览器的位置再启动谷歌。
from selenium import webdriver
import subprocess
# from selenium.webdriver.support import expected_conditions as EC
# from selenium.webdriver.support.ui import WebDriverWait
# from selenium.webdriver.common.by import By
import time
import winregkey winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe)
chrome_path winreg.QueryValue(key, )url https://steamdb.info/
# 结束已经启动的谷歌游览器
subprocess.run(taskkill /f /im chrome.exe, shellTrue)
# 启动谷歌
process subprocess.Popen([chrome_path, url])
# 等待用户手动关闭游览器用户等待cf通过再关闭游览器即可
process.wait()
# debug启动谷歌
subprocess.Popen([chrome_path, url, --remote-debugging-port9222])
# 接管debug游览器
option webdriver.ChromeOptions()
option.add_experimental_option(debuggerAddress, 127.0.0.1:9222)
browser webdriver.Chrome(optionsoption)
browser