Java自动化测试系列[v1.0.0][修改页面元素属性]

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);
    }




}

你可能感兴趣的:(Java自动化测试,修改页面元素属性)