Selenium Webdriver [Java] [gecko]:如何将字符串传递到警报中的输入字段 [英] Selenium Webdriver [Java] [gecko] : How do I pass a string to a input field in an Alert

查看:74
本文介绍了Selenium Webdriver [Java] [gecko]:如何将字符串传递到警报中的输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在线Web应用程序,其中列出了带有查看"按钮的产品.单击查看按钮时,将显示警报.警报具有四(4)个字段,例如:警报文本,输入文本字段,确定按钮和.取消按钮.警报文本显示为警报文本为:产品缺货!请输入您的电子邮件ID".

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

如果我使用Selenium Java代码取消了警报,则效果很好:

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

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

如果我只用我的Selenium Java代码单击确定",则效果很好:

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

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

但是,如果我尝试在警报的输入文本"字段中提供我的电子邮件ID,

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显示org.openqa.selenium.WebDriverException如下:

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

警报文本为:产品缺货!请输入您的电子邮件id org.openqa.selenium.WebDriverException:缺少值"参数(警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:6毫秒

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详细信息:"Windows 8"专业版,os.arch:"amd64",os.version:"6.2",

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

Java.version:"1.8.0_77"

Java.version: '1.8.0_77'

驱动程序信息:org.openqa.selenium.firefox.FirefoxDriver

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

有人可以帮我吗?

奇怪的是:当我尝试通过独立的Selenium Java类处理警报(即在警报文本框中传递emailID)时,这段代码可以正常工作.

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");

但是,当通过FRAMEWORK(由我实现)[Class:PlaceOrder,Method:orderCamera()]实现相同的代码时,则永远不会在Alert的Textbox字段中写入emailID.

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.

推荐答案

您需要将电子邮件发送到特定的文本字段

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

然后切换回

driver.switchTo().defaultContent();

这篇关于Selenium Webdriver [Java] [gecko]:如何将字符串传递到警报中的输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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