Java+Selenium3方法篇27-利用JS处理元素高亮显示
来源:互联网 发布:汉王全屏幕软件下载 编辑:程序博客网 时间:2024/06/05 07:44
这篇来玩一个好玩的测试,就是利用javascript让测试过程中的元素高亮显示,例如在登录框,让输入框背景颜色设置成黄色,边框设置2个像素,颜色是红色。这个问题,我在一家公司的笔试题上看见过,所以这里就实现了测试下。相关脚本如下。
package lessons;import java.util.concurrent.TimeUnit;import org.openqa.selenium.By;import org.openqa.selenium.JavascriptExecutor;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;public class ElementOpration {public static void main(String[] args) throws Exception { System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.get("https://www.baidu.com/"); Thread.sleep(2000); // 点击登录 driver.findElement(By.xpath("//*[@id='u1']/a[7]")).click(); Thread.sleep(500); WebElement username = driver.findElement(By.id("TANGRAM__PSP_8__userName")); // 创建一个JavascriptExecutor对象 JavascriptExecutor js= (JavascriptExecutor)driver; username.sendKeys("abcdefg"); // 设置颜色 js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');",username); WebElement password = driver.findElement(By.id("TANGRAM__PSP_8__password")); js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');",password); } }运行结果:输入过程不会变颜色,输入结束,里面变成我们想要的红色边框和黄色背景。这个效果,好像在公司里面给别人颜色demo,看起来很酷,其他的好像没有多大用处。
阅读全文
0 0
- Java+Selenium3方法篇27-利用JS处理元素高亮显示
- Java+Selenium3方法篇22-利用Javascript处理竖向滚动条
- Java+Selenium3方法篇15-元素内清除文本操作
- Java+Selenium3方法篇31-利用Robot类截图
- Java+Selenium3方法篇42-利用POI读写excel文件
- Java+Selenium3方法篇44-利用opencsv读取csv文件
- Java+Selenium3方法篇26-处理Alert弹窗
- Java+Selenium3方法篇32-处理不安全连接
- Java+Selenium3方法篇34-如何处理日历控件
- Java+Selenium3方法篇37-Robot类处理文件上传
- Java+Selenium3方法篇38-AutoIt工具处理文件上传
- Java+Selenium3方法篇25-isDisplayed方法
- Java+Selenium3方法篇14-元素操作之输入和点击
- Java+Selenium3方法篇46-自定义类库解决元素同步问题
- Java+Selenium3方法篇43-利用Jexcel操作2003版本excel
- Java+Selenium3方法篇13-findElements举例
- Java+Selenium3方法篇39-Explicit wait
- Java+Selenium3方法篇41-FluentWait
- VBA的代码,在Excel启动时自动执行
- UNIX System Overview
- 三次握手;为什么不用两次握手呢?
- C语言 身份证号解析年龄和性别
- 中断
- Java+Selenium3方法篇27-利用JS处理元素高亮显示
- 如何搭建SVN版本控制系统
- Linux基础
- UILabel赋值
- 计算机操作系统-进程的调度算法
- CPU调度
- 记录日常使用中JS的常用方法
- 【动态规划20】bzoj4818[sdoi2017]序列计数(dp+矩阵快速幂)
- angular缓存及多个controller间的共享