如何在单独的选项卡中从剪贴板复制打开链接

时间:2018-04-23 13:46:44

标签: java selenium

我有一个任务:点击链接复制到剪贴板的项目,点击后,此链接应该在单独的标签中打开(使用 Ctrl + V ) 。我使用Action但没有帮助

13px

我很感激你的建议。谢谢!

3 个答案:

答案 0 :(得分:0)

您可以在点击事件

后使用Javascripts执行者
JavascriptExecutor js = (JavascriptExecutor) driver; 
 js.executeScript("window.open('https://www.google.com','_blank');");

答案 1 :(得分:0)

当您点击链接时,新标签页面正在打开,因此您需要切换 WebDriver 的焦点,以便在新打开的标签页上执行任何操作。

 public static void main(String args0){

System.setProperty("webdriver.chrome.driver", "F:\\Automation\\chromedriver.exe");
WebDriver driver = new ChromeDriver();

  //Some operations on Page 1  (Page 1 is cuurent Page that you are working).
 //Click on link on Page 1.

  ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
  driver.switchTo().window(tabs.get(1));

  // performs some operations on Page 2
    Actions actions = new Actions(delegate);
    actions.sendKeys(Keys.chord(Keys.LEFT_CONTROL, "v")).build().perform(); 
    pauseFor(5000);

   driver.close();
   driver.switchTo.windows(tabs.get(0));
}

答案 2 :(得分:0)

使用系统剪贴板解决了问题:

&#13;
&#13;
        FluentWebElement urlButton = within(secs(5)).button(By.className("btn btn-link table-text table-text ft-link"));
        urlButton.click(); //copied to clipboard
        
        
        //get copied string from clipboard
                    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            String paste = (String) clipboard.getContents(null).getTransferData(DataFlavor.stringFlavor);
            
            //open in separate tab
             JavascriptExecutor js = (JavascriptExecutor) delegate;
            js.executeScript("popup_window = window.open('"+ paste+ "');");
            pauseFor(3000);
            js.executeScript("popup_window.close()");           
&#13;
&#13;
&#13;