首页 > 解决方案 > how to properly call the edge browser with selenium?

问题描述

The target of this project is to automate checking sites with Microsoft edge browser using selenium-python i downloaded the webdriver for the edge legacy from this link and i went for the latest release 17134 extracted it with out any problems now lets say i want to visit facebook in an automated way with firefox using the geckodriver

firefox code sample with selenium

from  selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options

# setting up headless option for faster execution
options = Options()
options.headless = True


browser = (webdriver.Firefox(options=options))
browser.get('https://www.facebook.com/')

but when I try to use Microsoft edge that is built in windows 10 I get an attribute error

from  selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.edge.options import Options



options = Options()
options.headless = True

#browser = webdriver.edge(options=options)
browser = webdriver.edge()

ps : when I uncomment this part (browser = webdriver.edge(options=options)) I get module not found error

what is the right way to call Microsoft edge browser , or what I am doing wrong

标签: python-3.xseleniummicrosoft-edge

解决方案


当我使用 Edge 并尝试使 Edge 无头时。我也发现很难做到这一点,因为 Chrome 的轻微变化。我参考了官方文档并获得了官方解决方案。除了 selenium,还需要安装msedge-selenium-tools,pip install 即可pip install msedge-selenium-toolsEdge并在 msedge 工具中使用Class。就像:

from msedge.selenium_tools import Edge
driver = Edge(executable_path='where')

如果我们想让 Edge 无头,我们需要使用EdgeOptionsselenium.webdriver 不提供的 Class。selenium.webdriver 只为我们提供了 ChromeOptions、FirefoxOptions 和 Ie 的。EdgeOptions 在一个单独的包中msedge.selenium_tools。然后我们添加参数,就像我们在 Firefox 或 Chrome 上所做的那样。在此之前,我们需要将属性use_chromium设置为 True。整个代码:

from msedge.selenium_tools import EdgeOptions
from msedge.selenium_tools import Edge

# make Edge headless
edge_options = EdgeOptions()
edge_options.use_chromium = True  # if we miss this line, we can't make Edge headless
# A little different from Chrome cause we don't need two lines before 'headless' and 'disable-gpu'
edge_options.add_argument('headless')
edge_options.add_argument('disable-gpu')

driver = Edge(executable_path='where', options=edge_options)

希望能帮助到你。对不起,我的尴尬解释。


推荐阅读