【问题标题】:Switch windowHandles and press Enter切换 windowHandles 并回车
【发布时间】:2018-01-16 14:39:24
【问题描述】:

我想在所有选项卡之间切换并按 ENTER 键。

web.SwitchTo().Window(web.WindowHandles[windowCounter]); javascript.ExecuteScript("$('button').click();");

在 for 循环中。 如果我只使用 SwitchTo 循环而不执行 Javascript,则在所有选项卡之间切换需要 7 毫秒。

当我使用 js 时,它似乎正在等待完成加载文档。我怎样才能快速切换选项卡并按回车键而不等待加载?

【问题讨论】:

    标签: javascript c# jquery selenium webdriver


    【解决方案1】:

    您需要将功能pageLoadStrategy 设置为none。但是,ChromeDriver 似乎不支持DesiredCapability。您可能需要一种解决方法。下面的示例使用RemoteWebDriver 设置DesiredCapability。它将打开一个空白的 Chrome 并首次将其关闭。

    string binLocation = @"./";
    ChromeOptions chromeOpt = new ChromeOptions();
    chromeOpt.AddArguments("--disable-extensions");
    ChromeDriverService service = ChromeDriverService.CreateDefaultService(binLocation);
    service.Port = 9515;
    var driver = new ChromeDriver(service, chromeOpt);
    driver.Close();
    
    var options = new Dictionary<string, object>();
    options.Add("browserName", "chrome");
    options.Add("pageLoadStrategy", "none");
    var capabilities = new DesiredCapabilities(options);
    var driver = new RemoteWebDriver(new Uri("http://localhost:9515"), capabilities);
    
    // do your works here //
    ////////////////////////
    
    web.SwitchTo().Window(web.WindowHandles[windowCounter]);
    // Make sure the button is clickable. You may use WebDriverWait.
    javascript.ExecuteScript("$('button').click();");
    

    【讨论】:

    • 你确定它会很快吗?它只需在 4 个选项卡中单击此按钮,每个选项卡的 rangetime 为
    • @TNation 它不会等待页面加载,因此 rangetime 应该小于 1 毫秒。
    • 我已经打开了一个驱动程序。驱动程序等待页面加载。我可以在运行时禁用等待吗?
    • @TNation 不,你不能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    相关资源
    最近更新 更多