分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

Selenium UI 举例 getCssValue

发布时间:2023-09-06 02:17责任编辑:胡小海关键词:暂无标签

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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved