zoukankan      html  css  js  c++  java
  • 使用JavascriptExecutor改变页面元素

    如下如html的页面代码

    <html>
        <body>
            <input type="text" name="text" value="alone">
        </body>
    </html>

    使用下列脚本即可改变标签的属性

    public class Selenium {
        public static WebDriver jsDriver;
        @BeforeMethod
        public void intiDriver(){
            System.setProperty("phantomjs.binary.path","D:\java\ideaWorkStation\casual\src\main\resources\driver\phantomjs.exe");
            jsDriver=new PhantomJSDriver();
            jsDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
            jsDriver.get("C:\Users\win7\Desktop\select.html");
    
        }
        @Test(enabled = true)
        public void javaScriptExcutor(){
            WebElement textEle=jsDriver.findElement(By.name("text"));
            System.out.println(textEle.getAttribute("value"));//输入改变之前的值
            String jsStrToSetAtt="arguments[0].setAttribute(arguments[1],arguments[2])";//改变属性的js
            ((JavascriptExecutor) jsDriver).executeScript(jsStrToSetAtt, textEle, "value", "no alnon");
            System.out.println(textEle.getAttribute("value"));//输入改变后前的值
            String jsStrToRemoveAtt="arguments[0].removeAttribute(arguments[1],arguments[2])";//移除属性的js
            ((JavascriptExecutor) jsDriver).executeScript(jsStrToRemoveAtt, textEle, "value");
            System.out.println(jsDriver.getPageSource());//输出改变后的页面代码
        }
    }

    执行脚本后,会看到输出如下,说明元素的属性被修改了

    alone
    no alnon
    <html><head></head><body>
    <input type="text" name="text">
    </body></html>

  • 相关阅读:
    ORA-00600: internal error code, arguments: [kgl-no-mutex-held]
    MongoDB3.4版本配置详解
    java.lang.CharSequence cannot be resolved
    truncate表恢复
    ERROR 1045 (28000): Access denied for user 'mycat'@'localhost' (using password: YES)
    安装mysql-python
    pip virtualenv requirements
    mapreduce on yarn简单内存分配解释
    tez参数
    jstat命令的使用及VM Thread分析
  • 原文地址:https://www.cnblogs.com/xxyBlogs/p/5862325.html
Copyright © 2011-2022 走看看