Python 3.x Python Selenium脚本将同时打开Edge和IE,而不是仅打开Edge
下面是我从JSON文件读取的代码。打开几个URL并截图 问题是;首先它打开边缘,然后第二项打开,即我做错了什么Python 3.x Python Selenium脚本将同时打开Edge和IE,而不是仅打开Edge,python-3.x,selenium,selenium-webdriver,automated-tests,microsoft-edge,Python 3.x,Selenium,Selenium Webdriver,Automated Tests,Microsoft Edge,下面是我从JSON文件读取的代码。打开几个URL并截图 问题是;首先它打开边缘,然后第二项打开,即我做错了什么 import json from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities caps = DesiredCapabilities.EDGE caps['ignoreProtectedModeSettings'] = T
import json
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.EDGE
caps['ignoreProtectedModeSettings'] = True
with open('path\sites.json', encoding='utf-8') as s:
data = json.loads(s.read())
for site in data['sites']:
driver = webdriver.Edge('C:\\Python37-32\\drivers\\MicrosoftWebDriver.exe', capabilities=caps)
driver.get(data['sites'][site])
driver.get_screenshot_as_file('screenshot path')
driver.close()
目前,不可能在Edge instance上吃更多的午餐,显然在可预见的未来它将保持这种状态。这方面有一个悬而未决的问题 Edge只允许运行一个实例进行Web驱动程序测试。 可能使用if/when Edge支持多个配置文件来实现此功能 还将添加 我将以“按设计工作”结束此操作 将驱动程序初始化移到循环外,或在每个循环结束时关闭浏览器
driver = webdriver.Edge('C:\\Python37-32\\drivers\\MicrosoftWebDriver.exe', capabilities=caps)
for site in data['sites']:
driver.get(data['sites'][site])
driver.get_screenshot_as_file('screenshot path')
driver.quit()
或
推送是基于应用程序的。 一些web应用程序具有硬编码指令,用于启动单独的浏览器,即浏览器。比如,
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>HTA Test</title>
<hta:application applicationname="HTA Test" scroll="yes" singleinstance="yes">
<script type="text/javascript">
function openURL()
{
var shell = new ActiveXObject("WScript.Shell");
shell.run("Firefox http://www.google.com");
}
</script>
</head>
<body>
<input type="button" onclick="openURL()" value="Open Google in Firefox">
</body>
</html>
HTA试验
函数openURL()
{
var shell=新的ActiveXObject(“WScript.shell”);
shell.run(“Firefoxhttp://www.google.com");
}
作为一种解决方法,尝试将驱动程序
定义移出for
循环,以便每个脚本仅创建一次WebDriver实例。提示:也用driver.quit()
@Andersson:尝试过了。还是一样。我以前在IE上运行过同样的脚本,效果很好。现在唯一的区别是broswer change&driver。问题不是需要多个Edge实例。如果我将driver.quit()移动到FOR块中,那么它将关闭edge,并且在任何给定时间,edge的一个实例都将运行。但问题是,;脚本在打开边缘后打开IE!不确定为什么!
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>HTA Test</title>
<hta:application applicationname="HTA Test" scroll="yes" singleinstance="yes">
<script type="text/javascript">
function openURL()
{
var shell = new ActiveXObject("WScript.Shell");
shell.run("Firefox http://www.google.com");
}
</script>
</head>
<body>
<input type="button" onclick="openURL()" value="Open Google in Firefox">
</body>
</html>