[英]How to get the Email/Text entered into a field when there is no “value” attribute or text in the code snippet for Java Selenium
I want to Get the email id entered "arjun_123@gmail.com" using java code. 我想使用Java代码获取输入为“ arjun_123@gmail.com”的电子邮件ID。 I tried the below codes, but it always returned null.
我尝试了以下代码,但始终返回null。
What I really need is, I want to check whether email is successfully entered or not. 我真正需要的是,我想检查电子邮件是否成功输入。 Sometimes, email is not getting entered.
有时,没有输入电子邮件。 So I want to check whether field is empty or not and retrieve the text entered.
因此,我想检查字段是否为空并检索输入的文本。
email.getAttribute("innerText");
email.getAttribute("innerHTML");
email.getAttribute("textContent");
email.getText();
Pass your email xpath in this method. 通过此方法传递您的电子邮件xpath。 You will get entered text
您将输入文字
public String getTextofInputBox(By by) {
JavascriptExecutor js = (JavascriptExecutor) driver;
String value = (String)((js).executeScript("return arguments[0].value",driver.findElement(by)));
return value;
}
您拉回了错误的属性,您要使用的是值:
email.getAttribute("value");
<input>
element <input>
元素 As per the documentation HTML <input>
element is an interactive controls for HTML forms to accept data from the user. 根据文档, HTML
<input>
元素是HTML表单的交互控件,用于接受来自用户的数据。
Data validation helps us to ensure that user have filled in the desired fields in the correct format to ensure that submitted data will be successfully accepted by the Application/Web Server. 数据验证有助于我们确保用户以正确的格式填写了所需的字段,以确保提交的数据将被应用程序/ Web服务器成功接受。
As per the snapshot: 根据快照:
The <input>
element contains JavaScript validation through the attribute data-validate
which have 2 sub-validations as 'validate-email':true
and 'required':true
. <input>
元素包含通过属性data-validate
进行的JavaScript验证,该属性具有2个子验证,分别为'validate-email':true
和'required':true
。 These Javasript validations validates the text entered within the <input>
field. 这些Javasript验证将验证在
<input>
字段中<input>
的文本。
So once the Javasript validation completes you can extract the text you have entered in the EMAIL field ie arjun_123@gmail.com inducing WebDriverWait using the following solution: 因此,一旦Javasript验证完成,您可以使用以下解决方案提取您在EMAIL字段中输入的文本,即arjun_123@gmail.com诱导WebDriverWait :
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(email).getAttribute("value"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.