[英]Unable to switch between two browser windows using Selenium WebDriver
I am new to WebDriver, i am facing an issue on browser window switching. 我是WebDriver的新手,我在浏览器窗口切换方面遇到问题。 I googled for my query resolution and the answer i found best is still not working for me.
我用Google搜索查询解决方案,但我发现最好的答案仍然不适合我。
Here is my code : 这是我的代码:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeSuite;
public class FrameWorkBase {
public static WebDriver driver;
public static WebDriverWait wait;
public static String firstWindow,secondWindow;
@BeforeSuite
public void startDriver() throws Exception{
driver= new FirefoxDriver(); // this firefox window is to open survey
driver.manage().window().maximize();
wait=new WebDriverWait(driver, 40);
driver.get("http://www.cricinfo.com");
firstWindow=driver.getWindowHandle();
driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://translate.google.co.in/");
secondWindow=driver.getWindowHandle();
System.out.println("First window handle :" + firstWindow);
System.out.println("\n Second window handle :" + secondWindow);
driver.switchTo().window(firstWindow);
System.out.println("hello");
}
}
I am getting an error on execution as Unable to find window 'xyz' where 'xyz' is the name of first window. 我在执行时遇到错误,因为找不到窗口“ xyz”,其中“ xyz”是第一个窗口的名称。 Even i am printing the window name and it is displaying the same window for which it is displaying error.
即使我正在打印窗口名称,它也会显示与显示错误相同的窗口。
Please suggest me what i am doing wrong here. 请在这里建议我我在做什么错。 Thanks
谢谢
This is happening because you have reinitialized the driver instance. 这是因为您已经重新初始化了驱动程序实例。
driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://translate.google.co.in/");
This line has reinitialised your driver instance so what ever u try to do you won't find the window handle. 此行已重新初始化您的驱动程序实例,因此您尝试执行的操作都找不到窗口句柄。 If you are trying to work on both websites simultaneously, try to create another object of driver like
WebDriver driver2 = new FirefoxDriver();
如果要同时在两个网站上工作,请尝试创建另一个驱动程序对象,例如
WebDriver driver2 = new FirefoxDriver();
@Vivek has aptly answered your question. @Vivek恰当地回答了您的问题。 But, if you still want to open a link in a new window, you can try the below code for that:
但是,如果您仍然想在新窗口中打开链接,则可以尝试以下代码:
Actions act = new Actions();
WebElement link = driver.findElement(By.xpath("//xpath of the link"));
//Opening the link in new window (works in FF and Chrome)
act.contextClick(link).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
And you can switch in between them accordingly, with the use of handles. 您可以使用手柄在它们之间进行相应切换。 Furthermore, this link will help you handle two windows simultaneously .
此外, 此链接将帮助您同时处理两个窗口 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.