WebElement 中 Selenium

WebElement 中 Selenium

表单是接收网站访问者信息的基本 Web 元素。Web 表单具有不同的 GUI 元素,如文本框、密码字段、复选框、单选按钮、下拉菜单、文件输入等。

我们将了解如何使用以下方式访问这些不同的表单元素 Selenium Web 驱动程序 Java. Selenium 将每一个表单元素封装成一个WebElement对象。 它提供 API 来查找元素并对其执行操作,例如在文本框中输入文本、单击按钮等。我们将看到可用于访问每个表单元素的方法。

WebElement、findElement()、findElements() 简介

Selenium Web Driver 将简单的表单元素封装为 Web元素。

WebDriver 使用多种技术根据 Web 元素的不同属性(如 ID、名称、类、XPath、标签名、CSS 选择器、链接文本等)来识别表单元素。

Web Driver 提供了以下两种 WebElement 方法来查找元素。

  • 查找元素() – 查找单个 Web 元素并返回 WebElement Selenium 目的。
  • 查找元素() – 返回符合定位器条件的 WebElement 对象列表。

让我们看一下使用 findElement() 方法获取网页中单个元素(文本字段)作为 WebElement 对象的代码片段。我们将在后续教程中介绍查找多个元素的 findElements() 方法。

步骤1) 我们需要导入此包来创建 Web Elements 对象

WebElement FindElement、FindElements 简介

步骤2) 我们需要调用 WebDriver 类上的 findElement() 方法并获取 WebElement 对象。

请参阅下文以了解如何完成。

Selenium 输入文本

输入框指以下两种类型之一:

  1. 文字字段– Selenium 输入文本框接受输入的值并按原样显示。
  2. 密码字段– 文本框接受输入的值,但将其屏蔽为一系列特殊字符(通常是点和星号),以避免显示敏感值。

    Selenium 输入文本

定位器

findElement() 方法接受一个参数,即元素的定位器。不同的定位器(如 By.id()、By.name()、By.xpath()、By.CSSSelector() 等)使用元素的属性(如 id、名称或路径等)来定位页面中的元素。

您可以使用诸如 Fire path 之类的插件来获取元素的 id、xpath 等。

使用示例站点 https://demo.guru99.com/test/login.html 下面给出的是使用 id 定位器定位“电子邮件地址”文本字段和使用名称定位器定位“密码”字段的代码。

定位器

  1. 电子邮件文本字段通过 Id 定位
  2. 密码字段位于名称处

发送密钥 Selenium

sendkeys() 在 Selenium 是一种在测试执行期间在文本和密码字段中输入可编辑内容的方法。这些字段使用诸如名称、类、ID 等定位器进行标识。它是一种在 Web 元素上可用的方法。与 type 方法不同,sendkeys() 方法不会替换任何文本框中的现有文本。

如何在 Selenium

要在文本字段和密码字段中输入文本,可以使用 sendKeys() 方法来在 WebElement 中 Selenium.

使用同样的例子 https://demo.guru99.com/test/login.html 站点,下面是我们如何找到文本字段和密码字段并在其中输入文本 Selenium.

文字输入 Selenium

  1. 使用 id 定位器找到“电子邮件地址”文本字段。
  2. 使用名称定位器找到“密码”字段
  3. 使用 Selenium sendkeys 方法。
  4. 使用 sendKeys() 方法在“密码”字段中输入密码。

删除输入中的值 Boxes

明确() 方法用于删除输入框中的文本。 此方法不需要参数。以下代码片段将清除电子邮件或密码字段中的文本

删除输入中的值 Boxes

Selenium 点击按钮

此 Selenium 可以使用 click() 方法访问点击按钮。

在上面的例子中

  1. 找到登录按钮
  2. 点击网站登录页面的“登录”按钮,登录网站。

Selenium 点击按钮

Selenium 提交按钮

提交按钮用于将整个表单提交到服务器。我们可以像上面一样在 Web 元素上使用 click () 方法(就像普通按钮一样),也可以在表单中的任何 Web 元素或提交按钮本身上使用 submit () 方法。

Selenium 提交按钮

当使用 submit() 时,WebDriver 将查找 DOM 以了解该元素属于哪个表单,然后触发其提交函数。

完整的代码

这是完整的工作代码

import org.openqa.selenium.By;		
import org.openqa.selenium.WebDriver;		
import org.openqa.selenium.chrome.ChromeDriver;		
import org.openqa.selenium.*;		

public class Form {				
    public static void main(String[] args) {									
    		
    	// declaration and instantiation of objects/variables		
        System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");					
        WebDriver driver = new ChromeDriver();					
        		
        String baseUrl = "https://demo.guru99.com/test/login.html";					
        driver.get(baseUrl);					

        // Get the WebElement corresponding to the Email Address(TextField)		
        WebElement email = driver.findElement(By.id("email"));							

        // Get the WebElement corresponding to the Password Field		
        WebElement password = driver.findElement(By.name("passwd"));							

        email.sendKeys("abcd@gmail.com");					
        password.sendKeys("abcdefghlkjl");					
        System.out.println("Text Field Set");					
         
        // Deleting values in the text box		
        email.clear();			
        password.clear();			
        System.out.println("Text Field Cleared");					

        // Find the submit button		
        WebElement login = driver.findElement(By.id("SubmitLogin"));							
                    		
        // Using click method to submit form		
        email.sendKeys("abcd@gmail.com");					
        password.sendKeys("abcdefghlkjl");					
        login.click();			
        System.out.println("Login Done with Click");					
        		
        //using submit method to submit the form. Submit used on password field		
        driver.get(baseUrl);					
        driver.findElement(By.id("email")).sendKeys("abcd@gmail.com");							
        driver.findElement(By.name("passwd")).sendKeys("abcdefghlkjl");							
        driver.findElement(By.id("SubmitLogin")).submit();					
        System.out.println("Login Done with Submit");					
         
		//driver.close();		
        		
    }		
}

故障排除

如果在查找元素时遇到 NoSuchElementException(),则表示 Web 驱动程序访问该页面时未在页面中找到该元素。

  1. 使用 Firepath 或 Chrome 中的 Inspect Element 再次检查您的定位器。
  2. 检查您在代码中使用的值是否与 Firepath 中元素的值不同。
  3. 一些元素的某些属性是动态的。如果您发现值不同且在动态变化,请考虑使用 By.xpath() 或 By.cssSelector(),这些方法更可靠但更复杂。
  4. 有时,这也可能是一个等待问题,即 Web 驱动程序甚至在页面完全加载之前就执行了您的代码,等等。
  5. 使用隐式或显式等待在 findElement() 之前添加等待。

结语

  • 下表总结了访问上述每种元素类型的命令
元素 命令 描述
留言板 发送密钥() 用于在文本框中输入值
明确() 用于清除文本框的当前值
链接 点击() 用于点击链接并等待页面加载完成后再继续执行下一个命令。
提交按钮 提交()
  • WebDriver 允许在多个 SELECT 元素中选择多个选项。
  • 您可以在表单中的任何元素上使用 submit() 方法。WebDriver 将自动触发该元素所属表单的提交功能。

总结一下这篇文章: