python - 什么是 python 的 useAutomationExtension for selenium?

标签 python google-chrome selenium selenium-webdriver selenium-chromedriver

我正在尝试从我的办公环境运行基本的 selenium 脚本,该环境具有代理和防火墙设置。该脚本运行良好,除了在每次执行之前都会弹出一个窗口,提示“管理员禁用加载解压扩展”。这意味着我必须手动单击它才能继续,这违背了自动化的目的。 enter image description here

我在 google 上搜索并计算出了这个错误,看起来有一个 chrome 选项 useAutomationExtension 需要被禁用。我继续寻找 python 的正确语法(环境:Python 2.7-win32,运行 chrome 驱动程序 2.30.477700(0057494ad8732195794a7b32078424f92a5fce41)),但找不到合适的 chrome 开关/选项。

我还调查了这个:来自谷歌的 Chromium/Chrome 开关:https://chromium.googlesource.com/chromium/src/+/master/chrome/common/chrome_switches.cc 和彼得的色度开关列表:https://peter.sh/experiments/chromium-command-line-switches/

我模糊地尝试了 chrome_options.add_argument('--disable-useAutomationExtension') 但这也没有帮助。

所以,我需要你的指导和建议。请帮忙。

代码部分:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re, os

from selenium.webdriver.chrome.options import Options


class Sel(unittest.TestCase):
    def setUp(self):
        # self.driver = webdriver.Firefox()

        # Clean existing file before starting
        #############################################
        dlpath = "C:\Users\Baba\blacksheep_tracker.xlsm"

        if os.path.exists(dlpath):
            os.remove(dlpath)

        ############################################

        chrome_options = Options()
        chrome_options.add_argument("--cipher-suite-blacklist=0x0039,0x0033")
        chrome_options.add_argument("--disable-extensions")
        chrome_options.add_argument('--start-maximized')
        chrome_options.add_argument('--disable-useAutomationExtension')
        self.driver = webdriver.Chrome(chrome_options=chrome_options)

        self.driver.implicitly_wait(30)
        self.base_url = "https://monsanto365.sharepoint.com/teams/XYZ_Tracker.xlsm"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_sel(self):
        driver = self.driver
        ## Launch the download url and wait for the download to complete
        driver.get("https://monsanto365.sharepoint.com/teams/xyz_tracker.xlsm")
        print 'Loading complete'
        time.sleep(30)
        print '30 sec over'

    def is_element_present(self, how, what):
        try:
            self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e:
            return False
        return True

    def is_alert_present(self):
        try:
            self.driver.switch_to_alert()
        except NoAlertPresentException, e:
            return False
        return True

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally:
            self.accept_next_alert = True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)


if __name__ == "__main__":
    unittest.main()

编辑: 我也知道他们正在处理这个问题的官方谷歌答案,它与 devtools 命令和东西有关。由于它需要永远,我正在寻找任何临时解决方案或建议。链接:https://bugs.chromium.org/p/chromedriver/issues/detail?id=639

最佳答案

驱动程序会在 Chrome 中安装一个扩展程序来实现一些功能,例如截图。

可以使用 useAutomationExtension 选项禁用它:

from selenium import webdriver

capabilities = {
  'browserName': 'chrome',
  'chromeOptions':  {
    'useAutomationExtension': False,
    'forceDevToolsScreenshot': True,
    'args': ['--start-maximized', '--disable-infobars']
  }
}    

driver = webdriver.Chrome(desired_capabilities=capabilities)

关于python - 什么是 python 的 useAutomationExtension for selenium?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45099288/

相关文章:

javascript - WebDriverJs 的资源?

python - 描述时间序列 Pandas 中的差距

python - 如何让 urlopen 处理文件中的 url 列表?

html - 我的网站在本地主机上看起来很完美,但在线上传后看起来完全不同?

javascript - 使用主题蒙大拿,Wordpress 网站不会在 chrome 中滚动

ruby-on-rails - authlogic 在使用 selenium 驱动程序时无法与 capybara 一起工作

python - lxml的E-factory是否支持动态生成数据?

python - 如何在Python中发送和接收实时信号 `sigqueue()`?

css - 知道如何在 Firefox 和 IE 上使用可用的 webkit 填充吗?

java - 如何使用 selenium 选择不包含特定类的元素