Using JavascriptExecutor to sendKeys plus click on web element

后端 未结 1 1474
后悔当初
后悔当初 2021-02-10 03:23

I\'m trying to open a link in a new tab, then switch to that tab, in a Firefox browser, using selenium in Java. It\'s my understanding that in order to do this, I need to use a

1条回答
  •  不知归路
    2021-02-10 03:47

    try below code to open any link on page to new tab & switch to that tab. Perform operations there & go back to first tab for further execution.

    WebDriver driver = new FirefoxDriver();
            driver.get("http://stackoverflow.com/");
            WebElement e = driver.findElement(By.xpath(".//*[@id='nav-questions']"));       
            Actions action = new Actions(driver); 
            action.keyDown(Keys.CONTROL).build().perform(); //press control key
            e.click();
            Thread.sleep(10000); // wait till your page loads in new tab
            action.keyUp(Keys.CONTROL).build().perform(); //release control key
            driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "\t"); //move to new tab
            driver.navigate().refresh(); // refresh page
            driver.findElement(By.xpath(".//*[@id='hlogo']/a")).click(); //perform any action in new tab. I am just clicking logo
            driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "\t"); //switch to first tab
            driver.navigate().refresh(); 
            driver.findElement(By.xpath(".//*[@id='hlogo']/a")).click();// refresh first tab & continue with your further work.I am just clicking logo
    

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