selenium jar包中,在WebElement的接口中,
String getCssValue(String var1);
可以通过标签,获取对应的css值。具体要怎么用呢,如下:
WebElement bossname = driver.findElement(By.cssSelector("div.boss-info"));
bossname.getCssValue("font-size")
package main.java.aTestDirectory;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.support.ui.ExpectedCondition;import org.openqa.selenium.support.ui.WebDriverWait;import org.testng.annotations.Test;public class TestBoss { ???String url = "https://m.zhipin.com/weijd/v2/job/e869a3212cbae6f51XV_0t65EVs~"; ???//堃誊(上海) Senior Software engineer in Test ???@Test ???public void TestSpider() {// 爬虫 ???????System.out.println("------Begin--------------"); ???????System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe"); ???????WebDriver driver = new ChromeDriver(); ???????driver.get(url); ???????(new WebDriverWait(driver, 30)).until(new ExpectedCondition<Boolean>() { ???????????public Boolean apply(WebDriver dr) { ???????????????int index = dr.getPageSource().indexOf("boss-info"); ???????????????if (index != -1) { ???????????????????return true; // 找到,退出等待 ???????????????} else { ???????????????????return false; // 未找到,继续等待 ???????????????} ???????????} ???????}); ???????WebElement bossname = driver.findElement(By.cssSelector("div.boss-info")); ???????System.out.println("font-size = " + bossname.getCssValue("font-size")); ???????System.out.println("background = " + bossname.getCssValue("background")); ???????System.out.println("line-height = " + bossname.getCssValue("line-height")); ???????System.out.println("color = " + bossname.getCssValue("color")); ???????System.out.println("font-family = " + bossname.getCssValue("font-family")); ???????System.out.println(bossname.getText()); ???????System.out.println("------End--------------"); ???}}
打印出来的结果:
通过Chrome校验下:
Selenium UI 举例 getCssValue
原文地址:https://www.cnblogs.com/qianjinyan/p/9744237.html