简体   繁体   English

Java Selenium的代码片段中没有“值”属性或文本时,如何使电子邮件/文本输入字段

[英]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 资料验证

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.

相关问题 如何从硒中没有值属性的文本框中获取输入的文本 - How to get entered text from a textbox having no value attribute in selenium 在Selenium WebDriver中使用Java在ID或任何属性中不存在值时如何从禁用的文本字段中获取值 - How to get value from a disabled text field when the value is not present in ID or in any attribute using Java in Selenium WebDriver 在 Eclipse 中使用 Selenium WebDriver 和 java 代码在搜索字段中输入文本后如何按“Enter” - How to press 'Enter' once text is entered in the search field using Selenium WebDriver and java code in eclipse Selenium Webdriver - 跳转到下一个字段时输入的文本消失 - Selenium Webdriver - The entered text is vanished when jumping to the next field Selenium Webdriver如何在iframe所见即所得文本区域中输入文本 - selenium webdriver how to get text entered in iframe wysiwyg text area 如何从输入字段中获取属性值? Selenium - Java - How to get Attribute value form Input field ? Selenium - Java 如何在 Selenium Java 中使用 CTRL + v 在输入字段中粘贴文本值? - how to paste a text value in input field with CTRL + v in Selenium Java? Selenium:根据可见文本获取属性值? - Selenium: Get Attribute Value Based on Visible Text? Java/Selenium WebDriver:如何获取用户输入并将其插入到文本字段中? - Java/Selenium WebDriver: How to get user input and insert it in to a text field? 在Netbeans的文本字段中输入文本时如何启用按钮 - How to enable a button when text is entered in a text field in Netbeans
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM