简体   繁体   English

硒从另一个标签而不是活动标签中获取数据

[英]Selenium getting data from another tab instead of active one

We have a legacy remote system here that dont have webapi, webservice, ... 我们这里有一个传统的远程系统,没有webapi,webservice,...
then we need to do integration by Selenium. 那么我们需要通过Selenium进行集成。
We need open multiple tabs from one master tab to do the extraction but when changing to desired tab and getting value by a css selector it get always the result from the fisrt tab. 我们需要从一个主选项卡中打开多个选项卡来进行提取,但是当更改为所需的选项卡并通过css选择器获取值时,它总是从fisrt选项卡中获得结果。
Our system cant be opened on internet then i did the same with Google as an example, the same behaviour happens. 我们的系统无法在互联网上打开,然后我以Google为例进行了相同的操作,发生了相同的行为。 Its a bug or my fault ? 是错误还是我的错? Someone can see whats is wrong? 有人可以看到出什么问题了吗?
Below is a simplified version without error check, its minimal code. 下面是没有错误检查的简化版本,其最小代码。
Thanks a lot. 非常感谢。

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");
    elmQtdRes = driver.FindElement(By.CssSelector("div#resultStats"));
    string strNewStat = elmQtdRes.Text;
    Console.WriteLine("Stat for WEB:[" + strWebStat + "]"); // prints stat for web - OK
    Console.WriteLine("Stat for NEWS:[" + strNewStat + "]"); // prints stat for web - WRONG
}

WebDriver will not automatically switch focus to a new window or tab, so we have to tell it when to make the switch. WebDriver不会自动将焦点切换到新的窗口或选项卡,因此我们必须告诉它何时进行切换。

To switch to the new window/tab, try: 要切换到新窗口/标签,请尝试:

driver.SwitchTo().Window(driver.WindowHandles.Last());

Then, when you are finished on the new tab, close it and switch back to the primary window: 然后,在新选项卡上完成后,将其关闭并切换回主窗口:

driver.Close();
driver.SwitchTo().Window(driver.WindowHandles.First());

Then from here, you can continue your test. 然后,您可以从这里继续测试。

I'll add a quick disclaimer that I do not have Visual Studio installed on the machine I'm using at the moment and haven't tested the code above. 我将添加一个快速免责声明,即我目前正在使用的计算机上未安装Visual Studio,并且尚未测试上面的代码。 This should, however get you in the right direction. 但是,这应该可以使您朝正确的方向发展。

EDIT: Looking at your code a second time, this example above would replace the SendKeys() call to attempt to switch tab focus: 编辑:再次查看您的代码,以上示例将替换SendKeys()调用以尝试切换选项卡焦点:

IWebElement elmBdy = driver.FindElement(By.CssSelector("body"));
elmBdy.SendKeys(Keys.Control + "2");

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

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