问题
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 scroll.
this is c# code.
Used Chrome 79
Chrome option is
options.AddArguments("handlesAlerts=false");
options.AddArguments("--disable-infobars");
options.AddArguments("--no-sandbox");
options.AddArguments("--disable-background-networking");
options.AddArguments("--disable-component-extensions-with-background-pages");
options.AddArguments("--dns-prefetch-disable");
options.AddArguments("--ignore-certificate-errors");
options.AddArguments("--ignore-certificate-errors-spki-list");
options.AddArguments("--ignore-ssl-errors");
options.AddArguments("--allow-running-insecure-content");
options.AddArguments("lang=ko_KR");
if (this.driver.WindowHandles.Count > 1)
{
this.driver.SwitchTo().Window(this.driver.WindowHandles[1]);
}
Utils.sleep(3000);
((IJavaScriptExecutor)this.driver).ExecuteScript("window.scrollBy(0,500);");
回答1:
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.
回答2:
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());
来源:https://stackoverflow.com/questions/59743574/how-to-scroll-in-new-tab-in-selenium