📜  Selenium WebDriver-WebElement命令

📅  最后修改于: 2020-11-06 04:51:31             🧑  作者: Mango

Selenium WebDriver-WebElement命令

在继续本节之前,首先我们应该了解与WebDriver中的Web元素相关的基本术语。

什么是Web元素?

术语Web元素是指HTML元素。 HTML文档由HTML元素组成。它由一个开始标签,一个结束标签以及两者之间的内容组成。例如,HTML元素写为:内容

在WebDriver中,我们有几个常用的Web元素命令和操作。以下屏幕快照显示了Eclipse Web元素命令面板。


注意:要获取Web元素对象,我们必须将语句编写为:

WebElement element = driver.findElement(By.id("UserName"));

在此,UserName是id属性的值,用作所需Web元素的唯一标识。

给出了一些Selenium WebDriver最常用的WebElement命令。

1.清除命令

方法:

clear() : void

命令:

element.clear();

程式码片段:

WebElement element = driver.findElement(By.id("UserName"));
element.clear();
             
//Or can be written as
             
driver.findElement(By.id("UserName")).clear();

2. Sendkeys命令

方法:

sendKeys(CharSequence? KeysToSend) : void

命令:

element.sendKeys("text");

程式码片段:

WebElement element = driver.findElement(By.id("UserName"));
element.sendKeys("JavaTpoint");
             
//Or can be written as
             
driver.findElement(By.id("UserName")).sendKeys("JavaTpoint");

3.单击命令

方法:

click() : void

命令:

element.click();

程式码片段:

WebElement element = driver.findElement(By.linkText("javaTpoint"));
element.click();
             
//Or can be written as
             
driver.findElement(By.linkText("javaTpoint")).click();

4. IsDisplayed命令

方法:

isDisplayed() : boolean

命令:

element.isDisplayed();

程式码片段:

WebElement element = driver.findElement(By.id("UserName"));
boolean status = element.isDisplayed();
             
//Or can be written as
             
boolean staus = driver.findElement(By.id("UserName")).isDisplayed();

5. IsEnabled命令

方法:

isEnabled() : boolean

命令:

element.isEnabled();

程式码片段:

WebElement element = driver.findElement(By.id("UserName"));
boolean status = element.isEnabled();
             
//Or can be written as
             
boolean staus = driver.findElement(By.id("UserName")).isEnabled();
             
//Or can be used as
WebElement element = driver.findElement(By.id("userName"));
boolean status = element.isEnabled();
// Check that if the Text field is enabled, if yes enter value
if(status){
element.sendKeys("javaTpoint");
}

6. IsSelected命令

方法:

isSelected() : boolean

命令:

element.isSelected();

程式码片段:

WebElement element = driver.findElement(By.id("Sex-Male"));
boolean status = element.isSelected();
             
//Or can be written as
             
boolean staus = driver.findElement(By.id("Sex-Male")).isSelected();

7.提交命令

方法:

submit() : void

命令:

element.submit();

程式码片段:

WebElement element = driver.findElement(By.id("SubmitButton"));
element.submit();
             
//Or can be written as
             
driver.findElement(By.id("SubmitButton")).submit();

8. GetText命令

方法:

getText() : String

命令:

element.getText();

程式码片段:

WebElement element = driver.findElement(By.xpath("anyLink"));
String linkText = element.getText();

9. GetTagName命令

方法:

getTagName() : String

命令:

element.getTagName();

程式码片段:

WebElement element = driver.findElement(By.id("SubmitButton"));
String tagName = element.getTagName();
             
 //Or can be written as
             
 String tagName = driver.findElement(By.id("SubmitButton")).getTagName();

10. getCssValue命令

方法:

getCssvalue() : String

命令:

element.getCssValue();

11. getAttribute命令

方法:

getAttribute(String Name) : String

命令:

element.getAttribute();

程式码片段:

WebElement element = driver.findElement(By.id("SubmitButton"));
  String attValue = element.getAttribute("id"); //This will return "SubmitButton"

12. getSize命令

方法:

getSize() : Dimension

命令:

element.getSize();

程式码片段:

WebElement element = driver.findElement(By.id("SubmitButton"));
Dimension dimensions = element.getSize();
System.out.println("Height :" + dimensions.height + "Width : "+ dimensions.width);

13. getLocation命令

方法:

getLocation() : Point

命令:

element.getLocation();

程式码片段:

WebElement element = driver.findElement(By.id("SubmitButton"));
Point point = element.getLocation();
System.out.println("X cordinate : " + point.x + "Y cordinate: " + point.y);