Selenium getting data from another tab instead of active one

后端 未结 1 745
庸人自扰
庸人自扰 2021-01-26 04:48

We have a legacy remote system here that dont have webapi, webservice, ...
then we need to do integration by Selenium.
We need open multiple tabs from one master tab to

1条回答
  •  北恋
    北恋 (楼主)
    2021-01-26 05:26

    WebDriver will not automatically switch focus to a new window or tab, so we have to tell it when to make the switch.

    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. 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:

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

    0 讨论(0)
提交回复
热议问题