使用java的SeleniumWebDriver中的Windows处理

使用java的SeleniumWebDriver中的Windows处理,java,selenium-webdriver,window-handles,Java,Selenium Webdriver,Window Handles,我有一个场景如下 1. Login to the application 2. click on a button (say Buy) 3. This will take me to a new window opened with a new URL automatically 4. Perform actions in the new window 5. Quit 请提供这方面工作的准确代码。我尝试使用网站中的可用代码,但该代码对我无效您可以尝试以下模式:- Webdriver dr

我有一个场景如下

1. Login to the application
2. click on a button (say Buy)
3. This will take me to a new window opened with a new URL automatically
4. Perform actions in the new window
5. Quit

请提供这方面工作的准确代码。我尝试使用网站中的可用代码,但该代码对我无效

您可以尝试以下模式:-

   Webdriver driver = new ChromeDriver();
   driver.get("URL of application");
   driver.findElement(By.id("username").sendKeys("user1");
   driver.findElement(By.id("password").sendKeys("pass1");
   driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
   driver.findElement(By.xPath("xpath of button").click();
//现在,您可以切换到弹出窗口并相应地接受或取消它

   driver.switchTo().alert().accept();

   driver.quit();

如果您提供应用程序URL的SO社区,则只能提供完整的代码。

我希望上述代码适用于您。允许您切换到子窗口并执行操作。此外,它还有一个实践示例,提供了有关在窗口之间切换的提示信息,请参见。至少你应该提到你所采取的哪些步骤不适合你,以及出现的错误是什么。简单地复制和粘贴任何问题都不会让你有任何进展,并且会吸引反对票。我是selenium的新手,不是java背景。所以需要像你这样的专家的帮助。你提供的链接帮助我解决了我的问题。谢谢我想你误解了OP的问题,因为这里没有警报。我想你可能也没有正确理解。隐式等待应该在驱动程序初始化之后定义,并且您需要超过三秒的时间。您的前两个findelement语句可能会失败,因为没有定义隐式等待。@比尔,我非常明白这个问题,因为Meghasri的语句是一般性的,所以我的回答是一般性的,以便她可以根据需要进一步增强它。就2秒的隐式等待而言,这是一种示例,因为我们不知道应用程序及其行为,所以我只需输入任何数字。对于前2个findelements,如果页面刷新,则需要等待,否则无需等待。