Selenium WebDriver中最常用的Java脚本功能是什么?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Selenium WebDriver中最常用的Java脚本功能是什么?相关的知识,希望对你有一定的参考价值。

我熟悉Selenium WebDriver中使用的javascriptExecutor函数,例如如何单击元素,向下滚动和浏览页面。我想知道在Selenium中是否可以实现JS的其他功能?

答案

您也可以获取元素文本

 private String jsGetElementText(String cssSelector) {
            JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
            return (String) javascriptExecutor.executeScript(String.format(
                    "var element = document.querySelector("%s");
" +
                            "if(element != null){
" +
                            "    return element.innerhtml;
" +
                            "}else{
" +
                            "    return "";
" +
                            "}", cssSelector));
        }

拖放文件

    public void dropFile(File filePath, WebElement target, int offsetX, int offsetY) {
        if (!filePath.exists())
            throw new WebDriverException("File not found: " + filePath.toString());

        JavascriptExecutor jse = (JavascriptExecutor) driver;

        String JS_DROP_FILE =
                "var target = arguments[0]," +
                        "    offsetX = arguments[1]," +
                        "    offsetY = arguments[2]," +
                        "    document = target.ownerDocument || document," +
                        "    window = document.defaultView || window;" +
                        "" +
                        "var input = document.createElement('INPUT');" +
                        "input.type = 'file';" +
                        "input.style.display = 'none';" +
                        "input.onchange = function () {" +
                        "  var rect = target.getBoundingClientRect()," +
                        "      x = rect.left + (offsetX || (rect.width >> 1))," +
                        "      y = rect.top + (offsetY || (rect.height >> 1))," +
                        "      dataTransfer = { files: this.files };" +
                        "" +
                        "  ['dragenter', 'dragover', 'drop'].forEach(function (name) {" +
                        "    var evt = document.createEvent('MouseEvent');" +
                        "    evt.initMouseEvent(name, !0, !0, window, 0, 0, 0, x, y, !1, !1, !1, !1, 0, null);" +
                        "    evt.dataTransfer = dataTransfer;" +
                        "    target.dispatchEvent(evt);" +
                        "  });" +
                        "" +
                        "  setTimeout(function () { document.body.removeChild(input); }, 25);" +
                        "};" +
                        "document.body.appendChild(input);" +
                        "return input;";

        WebElement input = (WebElement) jse.executeScript(JS_DROP_FILE, target, offsetX, offsetY);
        input.sendKeys(filePath.getAbsoluteFile().toString());
        waitFor(ExpectedConditions.stalenessOf(input));
    }

更改字段的不透明度

    public void changeOpacityForUploadField() {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("document.querySelector("input[id*='upload']").style.opacity='1'");
    }

停止加载页面

public void stopPageLoading() {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("return window.stop");
    }

打开浏览器扩展名

public void openBrowserExtension(){
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("window.postMessage('clicked_browser_action', '*')");
}

等待页面加载

public void waitForPageLoad(){
        JavascriptExecutor js = (JavascriptExecutor) driver;
        waitForCondition().until(d->js.executeScript("return document.readyState").equals("complete"));
    }

以上是关于Selenium WebDriver中最常用的Java脚本功能是什么?的主要内容,如果未能解决你的问题,请参考以下文章

在Jmeter中使用Selenium WebDriver完成测试

java+selenium3-常用的WebDriver API

Python selenium webdriver 基本使用

python+selenium自动测试之WebDriver的常用API(基础篇一)

selenium 常用api

Selenium webdriver常用属性和方法