2017-08-08 39 views
0

我想在所有選項卡之間切換,然後按ENTER鍵。切換窗口句柄,然後按Enter鍵

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

在for循環中

。 如果我只是使用SwitchTo循環而沒有執行Javascript,則需要7ms在所有選項卡之間進行切換。

當我使用JS時,似乎它正在等待完成加載文檔。我怎樣才能快速切換選項卡,並按下輸入到所有他們,而無需等待負載?

回答

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();"); 
+0

你確定它會很快嗎?它只需在4個選項卡中單擊此按鈕,每個選項卡的範圍時間小於200毫秒 – TNation

+0

@TNation它不會等待頁面加載,因此範圍時間應小於1毫秒。 – Buaban

+0

我已經打開了一個驅動程序。驅動程序等待頁面加載。我可以在運行時禁用等待嗎? – TNation