Python-Selenium脚本同时打开Edge和IE,而不是仅打开Edge

时间:2018-08-15 07:43:39

标签: python-3.x selenium selenium-webdriver automated-tests microsoft-edge

下面给出的是我从JSON文件读取的代码。打开一些URL并截图。

问题是;首先,它打开Edge,然后第二项打开IE。我在做什么错了?

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()

2 个答案:

答案 0 :(得分:0)

当前,除了在Edge实例上吃午餐以外,其他所有方法都是不可能的,并且显然在可预见的将来它将保持这种状态。 microsoft edge issues

中有一个未解决的问题
  

Edge仅允许一个实例正在运行以进行Web Driver测试。   也许如果/当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()

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.quit()

答案 1 :(得分:0)

推送基于应用程序。 一些Web应用程序具有硬编码的指令以启动单独的浏览器,即Explorer。例如;

<!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>