硒按钮Click()或Submit()? [英] Selenium Button Click() or Submit()?

查看:98
本文介绍了硒按钮Click()或Submit()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下情况下需要帮助(使用Java):

Need help on following scenario(using Java):

手动执行以下操作:在父页面中填写了一些信息后,点击页面上的继续"按钮

Doing it manually like this: after filling in some info in a parent page, clicking Continue button on it,

<INPUT TYPE='button' VALUE='Continue' onClick='sendForm()'>

子窗口(UserConfirmationPage)从其父窗口弹出,单击子页面上的继续"按钮,将数据发布到服务器.

A child window(UserConfirmationPage) pops up with those info from its parent window, clicking the Continue button on the child page, posting the data to server.

<FORM NAME='userConf' ACTION='user.jsp' METHOD='post'> 
Do you want to continue?<BR>
<INPUT TYPE='button' VALUE='Continue' onClick='createUser()'> 
</FORM>

但是,当我使用Selenium Web Driver进行操作时,在父页面上,

However, when I do it using Selenium Web Driver, on the parent page,

btnContinue.submit() 

子页面会弹出父页面中的那些信息,就像我手动执行操作时得到的信息一样,但父页面不再存在.使用时

a child page pops up with those info from parent page just like what I got when I do it manually but parent does not exist any more. While using

btnContinue.click() 

一个空白的子页面被打开而没有从父页面获取任何信息,并且它还抱怨会话丢失".

a blank child page is opened without getting any info from parent page and it also complains "session is lost".

我也尝试过:

JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].click();",btnContinue);

 new Actions(driver).moveToElement(driver.findElement(By.xpath("//input[@value='Continue']"))).click().perform();

但没有任何效果.

似乎Submit()和Click()都无法模拟手动完成的操作.有什么主意吗?

It seems that neither Submit() nor Click() could simulate what was done manually. Any idea?

非常感谢!

推荐答案

尝试一下,这是最简单易用的代码,可帮助您了解父子场景.

Try this it is simplest and easiest code which help you to understand Parent and child scenario.

public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();
driver.get("http://demo.guru99.com/popup.php");

driver.findElement(By.xpath("html/body/p/a")).click();

// return the parent window name as a String
String parentWindow=driver.getWindowHandle();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

// Pass a window handle to the other window
for(String childWindow: driver.getWindowHandles())
{
System.out.println("child");

//switch to child window
driver.switchTo().window(childWindow);

//find an element and print text of it 
WebElement textLabel=driver.findElement(By.xpath("html/body/div[1]/h2"));
System.out.println(" text:  "+textLabel.getText());
driver.close();
}

System.out.println("Come to parent window");

//switch to Parent window
driver.switchTo().window(parentWindow);

//find an element and print text of it 
 WebElement logotext=driver.findElement(By.xpath("html/body/div[1]/h2"));
 System.out.println("text: "+logotext.getText());

driver.close();
}

这篇关于硒按钮Click()或Submit()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆