Java自动化测试系列(修改页面元素属性)
程序员文章站
2022-05-28 21:33:53
只有干货,源码奉上,请无限扩展...
package Util; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; /**
* Summary: add/modify/delete attribute of elements
*
* @author: davieyang
* @create: 2018-08-05 2:29
*/ public class ModifyElementsAttributeByJS { public void setAttribute(WebDriver driver, WebElement element, String attributeName, String value){ JavascriptExecutor js = (JavascriptExecutor) driver; /**
* 调用js修改页面元素的属性值arguments[0]-arguments[2]会由element,attributeName,value替换
*/ js.executeScript("arguments[0].setAttribute(arguments[1],arguments[2])", element,attributeName,value); } public void removeAttribute(WebDriver driver, WebElement element, String attributeName){ JavascriptExecutor js = (JavascriptExecutor) driver; /**
* 调用js修改页面元素的属性值arguments[0]-arguments[1]会由element,attributeName替换
*/ js.executeScript("arguments[0].removeAttribute(arguments[1],arguments[2])", element,attributeName); } }
本文地址:https://blog.csdn.net/dawei_yang000000/article/details/108019774