I am working on 2 URLS . I need to open the first URL ,perform some action and open the second URL.
openURL1() { perform action }
openURL2() { perform action } switch back to URL1
when I use driver.get or driver.navigate , URL1 gets closed. Is there anyway to retain the window with URL1 while opening URL2? I am working with selenium and JAVA
答案 0 :(得分:3)
If the second tab is not opened through an action that is performed in first tab, then you can use two driver objects, so that you'll have control of both the browsers with different driver objects.
WebDriver driver = new ChromeDriver();
driver.get("https://news.ycombinator.com");
WebDriver driver1 = new ChromeDriver();
driver1.get("https://www.google.com");
By this approach you can use driver to control URL1 and driver1 to control URL2.
答案 1 :(得分:1)
您可以使用javascript打开新标签页
GGPlot
答案 2 :(得分:1)
以下是您的问题的答案:
以下是工作代码块,用于打开URL http://www.google.com
,在控制台上打印Working on Google
,在新选项卡中打开http://facebook.com/
,在控制台中打印Working on Facebook
,关闭打开网址http://facebook.com/
的标签页,让您返回打开网址http://www.google.com
的标签页,最后关闭它。
System.setProperty("webdriver.gecko.driver", "C:\\your_directory\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
String first_tab = driver.getWindowHandle();
System.out.println("Working on Google");
((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");
Set<String> s1 = driver.getWindowHandles();
Iterator<String> i1 = s1.iterator();
while(i1.hasNext())
{
String next_tab = i1.next();
if (!first_tab.equalsIgnoreCase(next_tab))
{
driver.switchTo().window(next_tab);
System.out.println("Working on Facebook");
driver.close();
}
}
driver.switchTo().window(first_tab);
driver.close();
如果这回答你的问题,请告诉我。