selenium常用的js總結

來源:互聯網
上載者:User

標籤:

1、 對input執行輸入

 

直接設定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("arguments[0].getInnerHTML()", e);        return result;    }

 

3. 滾動到指定位置

為啥使用滾動? 因為如果頁面沒有完全顯示,element如果是在下拉之後才能顯示出來,只能先滾動到該元素才能進行click,否則是不能click操作

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

 

selenium常用的js總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.