Selenium driver.WindowHandles只返回一个忽略Firefox标签的句柄
无法在多个标签之间导航
请参阅以下代码
我遇到this问题并且答案解决了,但我的问题并不精确,然后再问我
回应帮助了我,但现在我仍然坚持这个问题的问题,另一个人也有here但没有答案的问题。
还有一个甚至是单独的窗口,但在chrome here中。
//-------------------------------------------------
public static void testab()
{
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("https://www.google.com/ncr");
IWebElement elmTxt = driver.FindElement(By.CssSelector("input#lst-ib"));
elmTxt.SendKeys("google" + Keys.Enter);
IWebElement elmQtdRes = driver.FindElement(By.CssSelector("div#resultStats"));
string strWebStat = elmQtdRes.Text;
IWebElement elmNewsLnk = driver.FindElement(By.CssSelector("a[href*='tbm=nws']"));
elmNewsLnk.SendKeys(Keys.Control + Keys.Return);
IWebElement elmBdy = driver.FindElement(By.CssSelector("body"));
elmBdy.SendKeys(Keys.Control + "2");
//-------------------------------------------------
driver = switchLastOpenedTab(driver); // works
elmQtdRes = driver.FindElement(By.CssSelector("div#resultStats"));
string strNewStatV1 = elmQtdRes.Text;
Console.WriteLine("Stat for NEWS(V1):[" + strNewStatV1 + "]");
Console.WriteLine("Stat for WEB:[" + strWebStat + "]");
//-------------------------------------------------
driver = switchTabByIndex(driver, 2); // throw an Exception
elmQtdRes = driver.FindElement(By.CssSelector("div#resultStats"));
string strNewStatV2 = elmQtdRes.Text;
Console.WriteLine("Stat for NEWS(V2):[" + strNewStatV2 + "]");
}
//-------------------------------------------------
public static IWebDriver switchTabByIndex(IWebDriver driver, int tabIndex){
// it dont get a list of tabs just the windows container
List<String> winTabs = new List<String>(driver.WindowHandles);
int idx = tabIndex - 1;
// because of comment above count here equal one
if (idx >= winTabs.Count) {
throw new Exception("This method dont work!!");
}
driver.SwitchTo().Window(winTabs[idx]);
return driver;
}
//-------------------------------------------------
public static IWebDriver switchLastOpenedTab(IWebDriver driver)
{
// it works but cant handle more than one new tab, cant navigate
driver.SwitchTo().Window(driver.WindowHandles.Last());
return driver;
}
//-------------------------------------------------