How to scroll in New tab in selenium

后端 未结 2 1180
广开言路
广开言路 2021-01-24 04:06

I opened a new tab by clicking something in selenium in c #. I want to scroll after changing to a new tab, but I get a timeout error.

I get a timeout message and no scro

相关标签:
2条回答
  • 2021-01-24 04:41

    Please use the below code it will work fine

     //Open link in new tab
     Actions act = new Actions(driver);
     act.KeyDown(Keys.Control).MoveToElement(elementToopenInNewTab).Click().Perform();
     // Switch to new tab
     driver.SwitchTo().Window(driver.WindowHandles.Last());
    
     //Scroll down in new tab
    
      IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
      js.ExecuteScript("window.scrollTo(0, document.body.scrollHeight)");
    
     //Move to first tab again 
      driver.SwitchTo().Window(driver.WindowHandles.First());
    
    0 讨论(0)
  • 2021-01-24 05:00

    you can do this by two steps, move to the new tab and do the scroll there.

    ArrayList<String> AllTabs = new ArrayList<String> (driver.getWindowHandles()); 
    driver.switchTo().window(AllTabs.get(1));
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("window.scrollBy(0 , window.innerHeight)");
    

    And you can close the tab after you finish.

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