【问题标题】:Python-Selenium script opens Edge and IE both instead of Edge onlyPython-Selenium 脚本同时打开 Edge 和 IE,而不是仅打开 Edge
【发布时间】:2019-01-22 02:21:44
【问题描述】:

下面是我从 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()

【问题讨论】:

  • 作为一种解决方法,尝试将 driver 定义移出 for 循环,以便每个脚本仅创建一次 WebDriver 实例。提示:也将driver.close() 替换为driver.quit()
  • @Andersson:试过了。还是一样。我之前用 IE 运行过这个相同的脚本,它运行良好。现在唯一的区别是浏览器更改和驱动程序。

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


【解决方案1】:

目前,在 Edge 实例上吃午饭是不可能的,显然在可预见的将来它会保持这种状态。 microsoft edge issues 中有一个未解决的问题

Edge 只允许一个实例运行以进行 Web 驱动程序测试。 也许如果/当 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()

【讨论】:

  • 问题不在于需要多个 Edge 实例。如果我将 driver.quit() 移动到 FOR Block 中,那么它将关闭边缘,并且在任何给定时间只有一个 Edge 实例正在运行。但问题是;脚本在打开边缘后打开 IE!不知道为什么!
【解决方案2】:

推送基于应用程序。 一些网络应用程序有一个硬编码指令来启动一个单独的浏览器,即资源管理器。例如;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-12
    • 2015-12-10
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多