우리는 이미, 위치 요소를 배운 바로 가장 일반적으로 사용되는 방법을 알고에 다음, 첫 번째 단계, 위치 후이 요소에서 작동, 또는 (버튼)을 클릭해야하거나 입력 (입력 상자)에 배치했습니다.
일반적인 방법 1.WebDriver
다음 첫 번째는 WebDriver 가장 일반적으로 사용되는 여러 가지 방법을 이해하기 :
- 분명 () 일반 텍스트.
- SendKeys 매크로 (* 값)의 아날로그 입력 키.
- 요소를 클릭) (클릭
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();
}
}
분명 () 메소드는 텍스트 입력 상자의 내용을 삭제하는 데 사용됩니다.
입력 상자에 키보드 입력 내용을 시뮬레이션에서 SendKeys () 메소드. 그러나 그 역할은 우리는 또한 업로드 된 파일을 지정하는 데 사용하는 경우에도 키보드 키를 전송하는 데 사용할, 수, 거기서 멈추지 않습니다.
클릭 () 메소드가 요소를 클릭 할 수 있습니다, 그것은 클릭 할 수있는 객체입니다 제공, 그것은에서 SendKeys () 메소드는 웹 페이지이 가장 일반적으로 사용되는 작업을위한 방법이다. 사실, 클릭 () 메서드는 버튼을 클릭하는 데 사용되지 않습니다, 당신은 당신이, 텍스트 / 이미지 링크를 클릭 박스, 라디오 버튼을 체크, 등등 드롭 다운 상자 수를 클릭 할 수 있습니다.
2. 다른 종래 방법
- () 제출
제출 () 메소드는 양식을 제출하기 위해 사용된다. 예를 들어, "입력"동작의 키워드 입력란을 검색 한 결과, 제출하여 시뮬레이션 () 메소드있다.
……
WebElement search_text = driver.findElement(By.id("kw"));
search_text.sendKeys("Selenium");
search_text.submit();
……
- 의 getSize () 요소의 크기를 반환합니다.
- gettext에 () 텍스트 요소를 가져옵니다.
- getAttribute를 (이름) 속성 값을 얻었다.
- 사용자가 제공하는 요소가 표시되는지 여부 () isDisplayed.
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();
}
}
인쇄 결과 :
(500, 22)
©2017 Baidu 使用百度前必读 意见反馈 京 ICP 证 030173 号 京公网安备 11000002000001 号
text
true