【问题标题】:MS way to (Python) code Edge session using Selenium 4使用 Selenium 4 (Python) 编码 Edge 会话的 MS 方式
【发布时间】:2022-01-20 23:42:54
【问题描述】:

操作系统 - Windows 10 硒 - 4 蟒蛇 - 3.9.7 浏览器 - MS Edge (v97) 边缘驱动程序 (v97)

虽然以下代码有效,尽管抛出了错误,但似乎(根据 MS)Selenium 4 方法是使用“来自 OpenQA.Selenium.Edge 命名空间的官方 EdgeOptions 和 EdgeDriver 类”:

import unittest
from selenium import webdriver
from selenium.webdriver.edge import service
from selenium.webdriver.edge.options import Options

class Login(unittest.TestCase):

    def setUp(self):
        
        s=service.Service(r'C:\Users\Path_to_driver\msedgedriver.exe')
        self.driver = webdriver.Edge(service=s)
        url = "https://my_website"
        self.driver.get(url)

问题是,作为 Python OO 和 Selenium 的新手,我在如何做到这一点上陷入困​​境,我的所有网络搜索都未能提供我能理解的答案。我什至找不到 OpenQA.Selenium.Edge、EdgeOptions 或 EdgeDriver,这不是一个好的开始! :-/

理想情况下,我想使用 Selenium 4 方法,所以如果有人能给我一些建议,我将不胜感激。 TIA

【问题讨论】:

  • “OpenQA.Selenium.Edge 命名空间”是一个 C#/.NET 概念。它不适用于 Python。如果它按原样工作,那么你就不用担心了。
  • 好的,谢谢蒂姆
  • 请问您是否有机会检查我的答案?如果您有任何其他问题,我很乐意为您提供帮助。

标签: python selenium selenium-webdriver microsoft-edge


【解决方案1】:

我同意@TimRoberts 提到的,但如果你想在Python 中实现你的要求,你也可以使用msedge-selenium-tools

简单代码:

from msedge.selenium_tools import Edge, EdgeOptions

# load the desired webpage
edge_options = EdgeOptions()
edge_options.use_chromium = True
edge_options.binary_location = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"

driver = Edge(options = edge_options, executable_path=r"C:\Users\Administrator\Desktop\msedgedriver.exe")
driver.get('<your website url>')

编辑

或者您可以使用 Selenium 中内置的 EdgeOptions。简单代码如下:

from selenium import webdriver
from selenium.webdriver.edge import service

edgeOption = webdriver.EdgeOptions()
edgeOption.use_chromium = True
edgeOption.add_argument("start-maximized")
edgeOption.binary_location = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
s=service.Service(r'C:\Users\Administrator\Desktop\msedgedriver.exe')
driver = webdriver.Edge(service=s, options=edgeOption)
driver.get("<your website url>")

【讨论】:

  • 许多解决方案都建议采用这种方法,但我认为它不适合 Selenium 4!?这来自 MS:如果您使用 Selenium 4,则不需要使用 Selenium Tools for Microsoft Edge。 Microsoft Edge 的 Selenium 工具仅适用于 Selenium 3。正是这一点让我绕着圈子尝试不同的事情,但惨遭失败。我希望确定一种 W3C 的方法,因为这似乎是方法。
  • 好吧,如果是这种情况,您可以使用 Selenium webdriver 中内置的 EdgeOptions。简单参考我编辑的答案。
猜你喜欢
  • 1970-01-01
  • 2022-06-20
  • 2022-10-23
  • 2021-08-12
  • 1970-01-01
  • 2022-08-04
  • 2022-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多