use*_*466 5 css java selenium selenium-webdriver
我正在练习使用cssGetValue方法从特定Web元素的CSS属性中检索值.
我有两个问题:
为什么cssGetValue方法返回值13px,哪个web元素执行实际引用的方法.1A.我想获得标记为"By ID"的部分的CSS属性.我应该如何修改我的代码,以便我可以获得id ="by-id"部分的CSS属性值?
我使用了driver.close()方法,但是在脚本完成后它不会关闭浏览器.请解释为什么在这种情况下driver.close()方法不起作用.
这是我的代码片段:
package wd_findElementBy;
import java.util.List;
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class SearchWebElements
{
WebDriver driver = new FirefoxDriver();
private String baseUrl= "http://docs.seleniumhq.org/docs/03_webdriver.jsp#introducing-the-selenium-webdriver-api-by-example";
@Test
public void findElements(){
driver.get(baseUrl);
try{
List<WebElement> elements = driver.findElements(By.id("by-id"));
System.out.println("number of elements: " + elements.size());
for(WebElement ele : elements){
System.out.println(ele.getTagName());
System.out.println("get the text for web element with id='by-id' ");
System.out.println("------------------------------------------------------------");
System.out.println(ele.getText());
System.out.println("------------------------------------------------------------");
System.out.println(ele.getAttribute("id"));
System.out.println(ele.getCssValue("font-size"));
}
}
finally{
//driver.close();
driver.quit();
}
}
}
Run Code Online (Sandbox Code Playgroud)Yi *_*eng 13
是的,一切都正确.
这是font-size
通过Firebug 找到的屏幕截图.
由于id应该是唯一的(至少对于此页面而言),因此您不需要findElements
查找具有id by-id
和循环的元素列表,而是使用findElement
直接获取元素.
try{
WebElement byId = driver.findElement(By.id("by-id"));
System.out.println(byId.getTagName());
System.out.println("get the text for web element with id='by-id' ");
System.out.println("------------------------------------------------------------");
System.out.println(byId.getText());
System.out.println("------------------------------------------------------------");
System.out.println(byId.getAttribute("id"));
System.out.println(byId.getCssValue("font-size"));
}
}
Run Code Online (Sandbox Code Playgroud)
Rip*_*sim 11
获取CSS值:
driver.findElement(By.id("by-id")).getCssValue("font-size");//similarly you can use other CSS property such as background-color, font-family etc.
Run Code Online (Sandbox Code Playgroud)
在完成脚本执行后退出/关闭浏览器:
driver.quit();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
76551 次 |
最近记录: |