-WebDriver Selenium API using common methods used

We've already learned positioned elements, positioned just need to operate on this element after the first step, positioning, or click (button), or inputs (input box), following on to know the most commonly used method.

1.WebDriver common method

The following first to understand WebDriver most commonly used several methods:

  • clear () Clear text.
  • sendKeys (* value) analog key input.
  • click (), click the element
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
 
public class BaiduDemo {
 
  public static void main(String[] args) {
 
    WebDriver driver = new ChromeDriver();
    driver.get("https://www.baidu.com/");
 
    WebElement search_text = driver.findElement(By.id("kw"));
    WebElement search_button = driver.findElement(By.id("su"));
 
    search_text.sendKeys("Java");
    search_text.clear();
    search_text.sendKeys("Selenium");
    search_button.click();
 
    driver.quit();
  }
}

clear () method is used to clear the contents of the text input box.

sendKeys () method for simulating keyboard input content to the input box. But its role does not stop there, we can also use it to send keyboard keys, and even use it to specify the uploaded file.

click () method can be used to click on an element, provided it is an object that can be clicked, it sendKeys () method is a method for Web pages two most commonly used operations. In fact, click () method is not only used to click a button, you can click it you can click any text / image links, check boxes, radio buttons, drop-down box and so on.

2. Other conventional methods

  • submit()

submit () method is used to submit the form. For example, after searching the keyword input box of "enter" operation, it is possible () method for simulating by submit.

……
WebElement search_text = driver.findElement(By.id("kw"));
search_text.sendKeys("Selenium");
search_text.submit();
……
  • getSize () returns the size of the element.
  • getText () Gets the text element.
  • getAttribute (name) to obtain the property value.
  • isDisplayed () whether the element is visible to the user is provided.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
 
public class BaiduDemo {
 
  public static void main(String[] args) {
 
    WebDriver driver = new ChromeDriver();
    driver.get("https://www.baidu.com/");
 
    //获得百度输入框的尺寸
    WebElement size = driver.findElement(By.id("kw"));
    System.out.println(size.getSize());
 
    //返回百度页面底部备案信息
    WebElement text = driver.findElement(By.id("cp"));
    System.out.println(text.getText());
 
    //返回元素的属性值, 可以是 id、 name、 type 或元素拥有的其它任意属性
    WebElement ty = driver.findElement(By.id("kw"));
    System.out.println(ty.getAttribute("type"));
 
    //返回元素的结果是否可见, 返回结果为 True 或 False
    WebElement display = driver.findElement(By.id("kw"));
    System.out.println(display.isDisplayed());
 
    driver.quit();
  }
}

Print Results:

(500, 22)
©2017 Baidu 使用百度前必读 意见反馈 京 ICP 证 030173 号 京公网安备 11000002000001 号
text
true

Guess you like

Origin www.cnblogs.com/zhizhao/p/11303182.html