java 如何从主窗口切换到弹出窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14098679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:03:41  来源:igfitidea点击:

How to switch from main window to popup window?

javaseleniumwebdrivermodal-dialog

提问by Code Enthusiastic

I'm not talking about the popups like alert, confirm or prompt dialogs. In the application if I click on a button popup gets opened. I am not able to switch the WebDriver to the popup window.

我不是在谈论像警报、确认或提示对话框这样的弹出窗口。在应用程序中,如果我点击一个按钮弹出窗口被打开。我无法将 WebDriver 切换到弹出窗口。

I've tried to use getWindowHandles() but it only returns the main window handle.

I also tried switchTo.window("windowname") but it didn't work.

我尝试使用 getWindowHandles() 但它只返回主窗口句柄。

我也试过 switchTo.window("windowname") 但它没有用。

采纳答案by Code Enthusiastic

//handle of the master window before clicking the link
String master = driver.getWindowHandle();

driver.findElement(By.linkText("Click me")).click();

//logic for waiting for the popup, checking the size to become greater than 1 or breaking after sometime to avoid the infinite loop.
int timeCount = 1;

do
{
   driver.getWindowHandles();
   Thread.sleep(200);
   timeCount++;
   if ( timeCount > 50 ) 
   {
       break;
   }
}
while ( driver.getWindowHandles().size == 1 );

//Assigning the handles to a set
Set<String> handles = driver.getWindowHandles();
//Switching to the popup window.
for ( String handle : handles )
{
    if(!handle.equals(master))
    {
         driver.switchTo().window(handle);
    }
}

回答by Prashant Shukla

Usually Modular Windows are part of the same DOM, unlike javascripts alerts. Only thing that sets them apart from rest of the page is that they are in different frame.

通常模块化 Windows 是同一个 DOM 的一部分,与 javascripts 警报不同。唯一将它们与页面其余部分区分开来的是它们位于不同的框架中

Try to see if this Modular Window lies inside a frame or iframe tag. If any of the parent is frame or iframe then you will have to change the context to that frame before you can performa any action on the Modal Window.

尝试查看此模块化窗口是否位于框架或 iframe 标记内。如果任何父级是 frame 或 iframe,那么您必须先将上下文更改为该帧,然后才能对模态窗口执行任何操作。

So find the frame do a driver.switchTo().frame()and then perform the action on the element you want to. Once the action is done, which would most probably bring you back to the main page. Use driver.switchTo().defaultContent()to bring focus back to main page.

所以找到框架做一个driver.switchTo().frame()然后在你想要的元素上执行动作。一旦操作完成,这很可能会将您带回主页。使用driver.switchTo().defaultContent()把焦点回到主界面。

ThisSO question will be helpful.

这个问题会很有帮助。

If this does not work it would be helpful to have a look at the page or its HTML.

如果这不起作用,查看页面或其 HTML 会有所帮助。

回答by Santiago Hernandez

Are you using pageobjects?

你在使用页面对象吗?

If you are using this, you will need to find the elements after the popup appears, because initElements will not initialize them if they are not visible when you first open the page.

如果你正在使用这个,你将需要在弹出窗口出现后找到元素,因为如果第一次打开页面时它们不可见,initElements 将不会初始化它们。

回答by Matthew Petty

Assuming your talking about a javascript alert.

假设您在谈论 javascript 警报。

final Alert a = driver.switchTo().alert(); 
a.accept(); 

or

或者

Execute JavaScript directly to handle the altert

直接执行JavaScript来处理altert

and

Maybe wait for the alert to show up

也许等待警报出现

回答by Abhishek_Mishra

As per webdriver this issue is fixed in 2.16 but still it does not work Support for window.ShowmodalDialog

根据 webdriver,此问题已在 2.16 中修复,但仍然不起作用 支持 window.ShowmodalDialog

You can use Java Robot classfor handling such cases.

您可以使用Java Robot 类来处理此类情况。

Example :

例子 :

Wait(5000); // Wait for model pop, 
    int keyInput[] =
    {
      KeyEvent.VK_S, KeyEvent.VK_E, KeyEvent.VK_L, KeyEvent.VK_E,
      KeyEvent.VK_N, KeyEvent.VK_I, KeyEvent.VK_U, KeyEvent.VK_M,
    };   

    Robot robot = new Robot();

    robot.keyPress(KeyEvent.VK_TAB);
    robot.keyPress(KeyEvent.VK_TAB);


    for (int i = 0; i < keyInput.length; i++)
    {    
      robot.keyPress(keyInput[i]);
      robot.delay(100);    
    }  

    robot.delay(1000); 
    robot.keyPress(KeyEvent.VK_TAB);
    robot.delay(1000);
    robot.keyPress(KeyEvent.VK_TAB);
    robot.delay(1000);
    robot.keyPress(KeyEvent.VK_TAB);

    robot.delay(1000);
    robot.keyPress(KeyEvent.VK_ENTER); // Save Btn 

The delay between events is necessary otherwise you will miss the events.

事件之间的延迟是必要的,否则您将错过事件。