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
178 views
in Technique[技术] by (71.8m points)

Selenium Webdriver [Java] [gecko] : How do I pass a string to a input field in an Alert

I have an online Web Application where there is a product listed which have a View button. On clicking the View button an alert is shown. The Alert have four (4) fields as: Alert Text, An Input Text field, OK button & Cancel button. The Alert text reads as "The Alert Text is :Product is out of Stock ! Please enter your Email Id".

If I Cancel the alert with my Selenium Java code, it works well:

//Switch to the Alert & Dismiss
driver.switchTo().alert().dismiss();

If I simply click OK with my Selenium Java code, it does works well:

//Click on Accept
driver.switchTo().alert().accept();

But if I try to provide my email id in the Input Text field on the Alert,

Thread.sleep(3000);
driver.switchTo().alert().sendKeys("debanjan.selenium@mymail.com");
//driver.switchTo().alert().sendKeys("debanjan");

//Click on Accept
driver.switchTo().alert().accept();

Selenium is showing org.openqa.selenium.WebDriverException as follows:

The Alert Text is :Product is out of Stock ! Please enter your Email Id org.openqa.selenium.WebDriverException: Missing 'value' parameter (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 6 milliseconds

OS Details: 'Windows 8' pro, os.arch: 'amd64', os.version: '6.2',

Java.version: '1.8.0_77'

Driver info: org.openqa.selenium.firefox.FirefoxDriver

Can anyone help me out please?

The weird thing is that: When I try through standalone Selenium Java Class to handle the Alert (i.e. passing the emailID in the Alert Text Box) this piece of code works fine.

driver.switchTo().alert().sendKeys("debanjan.selenium@gmail.com");

But when the same code is implemented through a FRAMEWORK (which I implemented) [Class : PlaceOrder, Method : orderCamera()], the emailID is never written in the Textbox field of the Alert.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You need to send the email to the specific text field

Alert alert = driver.switchTo().alert();
WebElement inputTextField = driver.findElement(...);
inputTextField.sendKeys("debanjan.selenium@mymail.com");
alert.accept();

And to switch back

driver.switchTo().defaultContent();

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

...