Java 如何使用 selenium WebDriver 在弹出窗口中插入文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18267960/
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
How to insert text into popup window using selenium WebDriver
提问by user2688505
I am trying to automate a web page using selenium WebDriver. I can move towards the webpages and can able to do all tasks on parent pages. But at one place I am getting a POPUP window which asks username and password. I am unable to identify the username and password text box id's, how can I find the element (TextBox) and send the username and password through selenium webDriver code.
我正在尝试使用 selenium WebDriver 自动化网页。我可以移动到网页,并且可以在父页面上完成所有任务。但在一个地方,我收到一个询问用户名和密码的弹出窗口。我无法识别用户名和密码文本框 ID,如何找到元素 (TextBox) 并通过 selenium webDriver 代码发送用户名和密码。
回答by me_digvijay
You need to switch to the new pop-up window to select its elements. Here is an example to switch to the new window:
您需要切换到新的弹出窗口以选择其元素。以下是切换到新窗口的示例:
String handle = driver.getWindowHandles().toArray()[1];
// 1 stands for the new window and 0 for the parent window
String handle = driver.getWindowHandles().toArray()[1];
// 1 代表新窗口,0 代表父窗口
driver.switchTo(handle);
Now you can select your elements present in this window.
现在您可以选择此窗口中显示的元素。
*Note: *To get back to original window you again need to switch back to it with 0
as the index.
*注意:*要返回原始窗口,您需要再次切换回它0
作为索引。
回答by Pavel Janicek
Maybe the popup window is HTTP basic authenticated site. If so, then you cannot use the window handles as stated in previous answer, but in that case you have to send the username and password directly to URL request:
也许弹出窗口是 HTTP 基本认证站点。如果是这样,那么您不能使用先前答案中所述的窗口句柄,但在这种情况下,您必须将用户名和密码直接发送到 URL 请求:
driver.get("http://username:[email protected]");
回答by Karunagara
Its working fine for me as I used like this...
它对我来说很好用,因为我像这样使用......
String handle = driver.getWindowHandles().toArray()[1].toString();
driver.switchTo().window(handle);
回答by Disha
You can try this by using your web URL,
您可以使用您的网址尝试此操作,
@Test
public void testlinkedin() throws Exception {
driver.get("https://www.linkedin.com/"); //Use your web URL
driver.findElement(By.linkText("Sign Up")).click();
driver.findElement(By.cssSelector("button.fb-btn")).click();
Thread.sleep(3000);
Set <String>handles = driver.getWindowHandles();//To handle multiple windows
firstWinHandle = driver.getWindowHandle();
handles.remove(firstWinHandle);
String winHandle=handles.iterator().next();
if (winHandle!=firstWinHandle){
secondWinHandle=winHandle;
driver.switchTo().window(secondWinHandle); //Switch to popup window
Thread.sleep(2000);
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys("Username");
driver.findElement(By.id("pass")).clear();
driver.findElement(By.id("pass")).sendKeys("Password");
driver.findElement(By.id("u_0_2")).click();
driver.findElement(By.name("__CONFIRM__")).click();}
Make sure to declare these string variables as,
确保将这些字符串变量声明为,
public String firstWinHandle;
public String secondWinHandle;