Selenium:如何将URL粘贴到一个新的选项卡上-Java



我正在努力想办法将文本/URL粘贴到Chrome中的新选项卡上。我在Linux上以无头模式运行测试。因此,我可以启动一个新的选项卡,但无法将URL粘贴到导航栏上。

为什么要粘贴URL?我必须点击一个按钮,它会给我URL,我必须在一个新的选项卡上启动URL

这是我试图让它工作的代码。

copyBtn.click(); //copying the URL
((JavascriptExecutor) driver).executeScript("window.open()");// launching a new tab
SeleniumUtils.switchBrowserTab(driver, 1);
Actions actions = new Actions(driver);
actions.sendKeys(Keys.COMMAND, "v").sendKeys(Keys.ENTER).build().perform(); //sending the paste command 
System.out.println(driver.getCurrentUrl());

系统输出正在打印about:blank,而不是粘贴的文本。我想这是因为我没有把注意力集中在导航栏上。我准备了很多博客,但似乎没有办法专注于导航栏。有人能谈谈如何实现这个用例吗?

此外,我手头没有URL,老实说,我不知道如何才能得到复制的URL。因为我没有在本地运行,所以我不能使用ToolKit。非常感谢。

这个我用了很多:

public void openNewTab(WebDriver driver) throws InterruptedException {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.open('');");
Thread.sleep(100);
}
public void gotoTab(WebDriver driver, int tabIndex) throws InterruptedException {
List<String> winHandles = new ArrayList<>(driver.getWindowHandles());
Thread.sleep(500);
driver.switchTo().window(winHandles.get(tabIndex));
}

WebDriver driver = ...;
openNewTab(driver);
gotoTab(driver, 1); // zero based
driver.get("...");

相关内容

最新更新