Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

Selenium Button Click() or Submit()?

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()'>

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>

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".

I also tried:

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

and

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

but nothing works.

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

Thanks a lot in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You did not specify the language, so I assume you're using JAVA:

// since new tab has been opened - need to switch to this tab
// get a list of the currently open windows
Set<String> allTabs = driver.getWindowHandles();

// save the window handle for the current window
String programTab = driver.getWindowHandle();

// switching to the Save tab
String saveTab = ((String) allTabs.toArray()[1]);
driver.switchTo().window(saveTab);
// set timeout

// Click button
driver.findElement(By.id("buttonId")).click();
// set timeout

// switch back to the program tab
driver.switchTo().window(programTab);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...