selenium-webdriver 添加 option启动excludeSwitches useAutomationExtension并且执行execute_cdp_cmd
在使用selenium-webdriver时,可以通过添加option来启动excludeSwitches和useAutomationExtension。excludeSwitches是用来排除Chrome浏览器自带的一些扩展和功能,可以提高运行速度和稳定性。useAutomationExtension则是用来开启Chrome浏览器的自动化扩展,以便更好地与selenium-webdriver进行交互。
以下是添加option的示例代码:
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
除此之外,还可以使用execute_cdp_cmd方法来执行Chrome DevTools Protocol的命令。这个方法可以在selenium-webdriver中执行DevTools的一些高级命令,比如截屏、调整窗口大小、模拟网络状况等等。
以下是使用execute_cdp_cmd方法的示例代码:
driver.execute_cdp_cmd('Emulation.setDeviceMetricsOverride', {
'width': 360,
'height': 640,
'deviceScaleFactor': 1,
'mobile': True
})
这个例子中使用了Emulation.setDeviceMetricsOverride命令来模拟移动设备的屏幕大小和分辨率。通过这种方式,我们可以在selenium-webdriver中更加灵活地控制浏览器的行为和状态
原文地址: https://www.cveoy.top/t/topic/gEut 著作权归作者所有。请勿转载和采集!