如何在Chrome浏览器上打开新标签页。我需要从中获取一些数据,返回到上一个选项卡并输入数据。我知道如何遍历选项卡,但是无法打开新的选项卡。

硒版本:3.5.2
镀铬版本:60

package Amazon;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class VerifyAmazonSignInPage {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver", "C://Selenium jars/chromedriver.exe");

        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.get("http://www.amazon.in");
        driver.findElement(By.xpath("//span[text()='Hello. Sign in']")).click();
        driver.findElement(By.id("ap_email")).sendKeys("[email protected]");
        driver.findElement(By.id("ap_password")).sendKeys("*****");
        driver.findElement(By.id("signInSubmit")).click();
        Actions act = new Actions(driver);
        act.keyDown(Keys.CONTROL).sendKeys("t").keyUp(Keys.CONTROL).build().perform();
        driver.get("http://www.gmail.com");


    }

}

最佳答案

动作类是硒类,只能用于使基于Web的应用程序自动化。打开新标签页是在Web浏览器上执行的操作,该浏览器是一个独立的应用程序。可以通过使用java.awt包中的Robot类来完成。

07-26 04:59