gpt4 book ai didi

selenium-webdriver - chromedriver headless 警报

转载 作者:行者123 更新时间:2023-12-04 00:41:11 24 4
gpt4 key购买 nike

我在使用 chromedriver 进行 selenium webdriver 测试时遇到了这个问题。虽然我可以在使用 Chrome 浏览器时成功运行测试,但我无法在 headless 模式下运行相同的测试。

我无法处理 Js 警报。实际上,在截屏时,似乎警报甚至都不会弹出。

Alert screenshot

我尝试了几种解决方法:

1) driver.window_handles --> 似乎没有其他窗口存在

2) driver.execute_script("window.confirm = function(){return true;}") --> 该脚本没有任何改变

3) element = WebDriverWait(driver, 20).until(EC.alert_is_present())当然还有明确的等待

在浏览器模式下,我使用普通的:

try:
print driver.switch_to.alert.text
driver.switch_to.alert.accept()
except NoAlertPresentException as e:
print("no alert")

还有其他人在 headless 模式下遇到警报这个问题吗?
  • chromedriver v.2.30.477691
  • Chrome 版本 59.0.3071.115
  • 最佳答案

    由于 Chrome headless (当前)不支持警报,因此您必须对 alert() 进行猴子补丁。和 confirm()方法。这是我使用的方法(在 C# 中):

        /// <summary>
    /// The Chrome Headless driver doesn't support alerts, so we need to override the window.alert method to get the expected behavior.
    /// </summary>
    /// <param name="driver">The active IWebDriver instance</param>
    /// <param name="result">The result that the alert should return, i.e., true if we want it "accepted", false if we don't</param>
    public static void SetupAlert(this IWebDriver driver, bool result)
    {
    // ks 7/27/17 - The Chrome Headless driver doesn't support alerts, so override the various window.alert methods to just set
    const string scriptTemplate = @"
    window.alertHandlerCalled = false;
    window.alertMessage = null;
    window.alert = window.confirm = function(str) {
    window.alertHandlerCalled = true;
    window.alertMessage = str;
    return {{result}};
    };";

    var script = scriptTemplate.Replace("{{result}}", result.ToString().ToLower());
    var js = (IJavaScriptExecutor)driver;
    js.ExecuteScript(script);
    }

    /// <summary>
    /// This is an optional accompaniment to the <see cref="SetupAlert"/> method, which checks to see
    /// if the alert was, in fact, called. If you don't want to bother to check, don't worry about calling it.
    /// Note that this doesn't reset anything, so you need to call <see cref="SetupAlert"/> each time before calling
    /// this method.
    /// </summary>
    public static void WaitForAlert(this IWebDriver driver, TimeSpan? timeout = null)
    {
    const string script = @"return window.alertHandlerCalled";
    var js = (IJavaScriptExecutor)driver;
    var timeToBail = DateTime.Now.Add(timeout ?? TimeSpan.FromMilliseconds(500));
    while (DateTime.Now < timeToBail)
    {
    var result = (bool)js.ExecuteScript(script);
    if (result) return;
    Thread.Sleep(100);
    }
    throw new InvalidOperationException("The alert was not called.");
    }

    我这样使用它:
            Driver.SetupAlert(true);
    this.ClickElement(ResetButton);
    Driver.WaitForAlert();

    关于selenium-webdriver - chromedriver headless 警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45242264/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com