selenium常用的js总结

直接设置value属性, 此方法主要应对输入框自动补全以及readonly属性的element,sendkeys不稳定

比如:

        //inputbox is a WebElement
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("arguments[0].value=\"北京\"", from_inpox);

对此可以封装一个typeQuick的方法

/**
     * @author Young
     * @param locator
     * @param values
     * @throws Exception
     */
    protected void typeQuick(Locator locator, String values) throws Exception {
        WebElement e = findElement(driver, locator);
        log.info("type value is:  " + values);
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("arguments[0].value=\""+values+"\"", e);

去掉只读属性

   JavascriptExecutor js = (JavascriptExecutor) driver;
   js.executeScript("arguments[0].removeAttribute(\"+"readonly"+\")", e);

 

2.对富文本框的操作

主要应对富文本框,可以封装获取富文本框内容和设置富文本路况内容的方法

JavascriptExecutor js = (JavascriptExecutor) driver;
        WebElement editor = driver.findElement(By.tagName("body"));
        js.executeScript(
                "arguments[0].innerHTML = '<h1>Selenium Test </h1>I love Selenium <br> this article Post By Selenium WebDriver<br><h2>Create By Young</h2>'",
                editor);

设置富文本框内容

    /**
     * @author Young
     * @param locator
     * @param text
     */
    protected void setRichTextBox(Locator locator, String text) {
        WebElement e = findElement(driver, locator);
        log.info("type value is:  " + text);
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("arguments[0].innerHTML = \"" + text + "\"", e);
    }

获取富文本框内容:

    /**
     * @author Young
     * @param locator
     * @param text
     * @return
     */
    protected String getRichTextBox(Locator locator, String text) {
        WebElement e = findElement(driver, locator);
        log.info("type value is:  " + text);
        JavascriptExecutor js = (JavascriptExecutor) driver;
        String result=(String) js.executeScript("var result=arguments[0].innerHTML;return result", e);
        return result;
    }

 


3. 滚动到指定位置

为啥使用滚动? 因为如果页面没有完全显示,element如果是在下拉之后才能显示出来,只能先滚动到该元素才能进行click,否则是不能click操作

1
2
3
JavascriptExecutor js=(JavascriptExecutor)driver;
        // roll down and keep the element to the center of browser
        js.executeScript("arguments[0].scrollIntoViewIfNeeded(true);", download);

可以封装滚动到元素的方法的

 

/**
     * @author Young
     * @param locator
     */
    protected void scrollToElement(Locator locator) {
        WebElement e = findElement(driver, locator);
        log.info("scroll view element");
        JavascriptExecutor js = (JavascriptExecutor) driver;
        // roll down and keep the element to the center of browser
        js.executeScript("arguments[0].scrollIntoViewIfNeeded(true);", e);
    }

 


4. 触发event,Dom event 事件的封装

    /**
     * For DOM Event 
     * @author Young
     * @param locator
     * @param event
     * please refer to: http://www.w3school.com.cn/jsref/dom_obj_event.asp
     * 
     */
    public void DOMEvent(Locator locator,String event)
    {
        JavascriptExecutor jse=((JavascriptExecutor)driver);
        String js="var event;if (document.createEvent){event = document.createEvent(\"HTMLEvents\");event.initEvent(\""+event+"\", true, false);arguments[0].dispatchEvent(event);} else {arguments[0].fireEvent(\"on"+event+"\")}";
        jse.executeScript(js, findElement(driver,locator)) ;
    }

 


五、获取元素属性

window.getComputedStyle(document.getElementById("su"),null).getPropertyValue("background")

 

六 、获取页面加载状态

document.readyState

 

操作滚动条
x为水平移动的像素、y为垂直移动像素、location为元素位置


1、操作垂直滚动条

String scroll = "document.documentElement.scrollTop=" + "y"; 
JavascriptExecutor js=(JavascriptExecutor) driver; 
js.executeScript(scroll);

2、操作水平滚动条

String scroll = "document.documentElement.scrollLeft=" + "x"; 
JavascriptExecutor js=(JavascriptExecutor) driver; 
js.executeScript(scroll);

3、同时操作2个坐标,水平移动到100像素,向下移动到500像素

((JavascriptExecutor)driver).executeScript("window.scrollTo(x,y)");

4、采用相对移动的方式

((JavascriptExecutor)driver).executeScript("window.scrollBy(x,y)");

5、如果操作的是div的scroll同样可以实现

String scroll = "document.getElementById("location").scrollLeft=x";
JavascriptExecutor js=(JavascriptExecutor)driver; 
js.executeScript(scroll);

—-需要注意的是你的id不要选错了,可以用xpath测试下是不是包含您的div内容(不能多,也不能少)。


操作富文本


String text = "text"; //输入的内容
String js = "var sum=document.getElementById('id'); sum.value='" + text + "';"; 
((JavascriptExecutor)driver).executeScript(js); 

操作Html5视频

JavascriptExecutor js=(JavascriptExecutor) driver; 
//获取视频url
js.executeScript("return arguments[0].currentSrc;",video);
//播放视频
js.executeScript("return arguments[0].play()", video); 
//暂停视频
js.executeScript("arguments[0].pause()", video);
赞 (0) 打赏

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏