我正在尝试通过Selenium中的Webdriver打开一个新的浏览器选项卡。但是,打开的新选项卡是空白的。我正在尝试将网站链接发送到Webdriver对象,但链接仍未打开。
这是我的代码:
// Assuming path is already set
WebDriver driver = new ChromeDriver();
String baseURL = "https://www.flipkart.com/";
driver.get(baseURL);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_T);
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_T);
Set<String> handles = driver.getWindowHandles();
Iterator itr = handles.iterator();
String parent_window = (String) itr.next();
System.out.println(parent_window + "....." + driver.getTitle());
String child_window = (String) itr.next();
driver.switchTo().window(child_window);
driver.navigate().to("https://www.flipkart.com/");
答案 0 :(得分:1)
试试这个。
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1)); //switches to new tab
driver.get("https://www.facebook.com");
driver.switchTo().window(tabs.get(0)); // switch back to main screen
driver.get("https://www.news.google.com");