selenium的十大定位方法

  1. By.id
  2. By.name
  3. By.className
  4. By.tagName
  5. By.LinkText
  6. By.xpath
  7. By.cssSeclector
  8. By.partialLinkText

常见元素处理

selenium的十大定位方法

基本代码演示

package com.test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.PageFactory;

/**
 * @author Huangtian
 * @create 2019-03-11 8:49
 */
public class Init_Page {
    /**
     * 页面工厂类
     * 所有的页面继承这个父类
     * 获得ChromeDriver
     */
    public WebDriver driver = new ChromeDriver();

    public Init_Page() {
        System.setProperty("webdriver.chrome.driver", "E:\\webdriver\\chromedriver.exe");
        PageFactory.initElements(driver, this);
    }
}
package com.test.demo;

import com.test.Init_Page;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

/**
 * @author Huangtian
 * @create 2019-03-11 8:51
 */
public class Input_Element extends Init_Page {
    /**
     * 分析input输入框元素 sendkeys;clear;getAttribute;
     */
    //用户名输入框
    @FindBy(id = "username")
    WebElement username;
    //密码输入框
    @FindBy(id = "password")
    WebElement password;

    @Test
    public void getUrl() {
        //输入网址
        driver.get("https://www.w3cschool.cn");
        //浏览器最大化
        driver.manage().window().maximize();
        //智能等待
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        //getAttribute()方法通过名称获得属性的值
        String username_Attribute = username.getAttribute("placeholder");
        String password_Attribute = password.getAttribute("placeholder");
        System.out.println(username_Attribute);
        System.out.println(password_Attribute);
        //往输入框内输入值
        username.sendKeys("ihuangtian");
        password.sendKeys("123");
        //获取输入框内的值
        String username_value = username.getAttribute("value");
        System.out.println(username_value);
        //清空输入框内的值
        username.clear();
        password.clear();
    }

}

 

相关文章: