我正试图导航到其他窗口。但firefox正在新标签中打开新页面,而不是新窗口。我勾选复选框并取消选中。
System.setProperty("webdriver.firefox.bin", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
System.setProperty("webdriver.gecko.driver", "C:\\Selenium Purely\\geckodriver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver(); //Launches FireFox browser with blank URL
driver.get("http://the-internet.herokuapp.com/windows");
Thread.sleep(4000);
String parentwindow = driver.getWindowHandle();
System.out.println("Parent window handle is "+parentwindow);
System.out.println("Parent window title is "+driver.getTitle());
driver.findElement(By.partialLinkText("Click Here")).click();
Set <String> s1 = driver.getWindowHandles();
Iterator <String> i1= s1.iterator();
while (i1.hasNext())
{
String childwindow = i1.next();
System.out.println("Child window handle is "+childwindow);
System.out.println("Child window title is "+driver.getTitle());
if(parentwindow != childwindow)
{
driver.switchTo().window(childwindow);
Thread.sleep(4000);
}
}
答案 0 :(得分:0)
好吧,要使用Selenium,您不需要在代码中添加Firefox exe的绝对文件路径,因为它将添加到您的系统PATH变量中。所以你可以省略这一行:
System.setProperty("webdriver.firefox.bin", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
现在,要使Firefox在新Windows中打开URL,有一些设置要做。就你的问题而言How to open a new window instead of new tab
这里是IE的工作代码:
package demo;
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class TestAnyURLMain {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.ie.driver", "C:\\Utility\\BrowserDrivers\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.manage().window().maximize();
driver.get("http://the-internet.herokuapp.com/windows");
Thread.sleep(4000);
String parentwindow = driver.getWindowHandle();
System.out.println("Parent window handle is "+parentwindow);
System.out.println("Parent window title is "+driver.getTitle());
driver.findElement(By.partialLinkText("Click Here")).click();
Set <String> s1 = driver.getWindowHandles();
Iterator <String> i1= s1.iterator();
while (i1.hasNext())
{
String childwindow = i1.next();
System.out.println("Child window handle is "+childwindow);
System.out.println("Child window title is "+driver.getTitle());
if(parentwindow != childwindow)
{
driver.switchTo().window(childwindow);
Thread.sleep(4000);
}
}
}
}
如果这有助于您,请告诉我。