繁体   English   中英

切换窗口句柄并按Enter

[英]Switch windowHandles and press Enter

我想在所有选项卡之间切换,然后按Enter键。

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

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

当我使用js时,似乎正在等待完成文档的加载。 我如何快速切换选项卡并按Enter即可不等待加载?

您需要将功能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();");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM