简体   繁体   English

如何使用 Selenium 和 Node.JS 驱动 Firefox 滚动到元素?

[英]How to scroll to element with Selenium and Node.JS driving Firefox?

I'm trying to scroll to a link element on the page and click it.我正在尝试滚动到页面上的链接元素并单击它。 The following solution works for Chrome and IE, but not for Firefox.以下解决方案适用于 Chrome 和 IE,但不适用于 Firefox。 How could I fix this?我怎么能解决这个问题? Is there any other approach to it?还有其他方法吗?

function clickByLinkTextScroll(text){
        driver.findElement(By.linkText(text)).getLocation().then(function (location) {
            driver.executeScript("window.scroll(0," + location.y + ");");
            driver.sleep(300);
            driver.findElement(By.linkText(text)).click();
        });
};

When I run it against Firefox I get the following output:当我对 Firefox 运行它时,我得到以下输出:

UnknownCommandError: GET /session/ad17a19d-782b-4706-b1d7-b56f0139b252/element/5c5e0c3d-1861-432c-b402-b6a010c2f268/location did not match a known command
    at WebDriverError (C:\ProgramData\npm\node_modules\selenium-webdriver\lib\error.js:27:5)
    at UnknownCommandError (C:\ProgramData\npm\node_modules\selenium-webdriver\lib\error.js:296:5)
    at Object.throwDecodedError (C:\ProgramData\npm\node_modules\selenium-webdriver\lib\error.js:477:11)
    at parseHttpResponse (C:\ProgramData\npm\node_modules\selenium-webdriver\lib\http.js:388:15)
    at doSend.then.response (C:\ProgramData\npm\node_modules\selenium-webdriver\lib\http.js:330:11)
    at process._tickCallback (internal/process/next_tick.js:103:7)
From: Task: WebElement.getLocation()
    at Driver.schedule (C:\ProgramData\npm\node_modules\selenium-webdriver\lib\webdriver.js:414:17)
    at WebElementPromise.schedule_ (C:\ProgramData\npm\node_modules\selenium-webdriver\lib\webdriver.js:1823:25)
    at WebElementPromise.getLocation (C:\ProgramData\npm\node_modules\selenium-webdriver\lib\webdriver.js:2112:17)
    at clickByLinkTextScroll (innovationCentralTests.js:694:43)
    at Context.<anonymous> (innovationCentralTests.js:445:9)
    at C:\ProgramData\npm\node_modules\selenium-webdriver\testing\index.js:153:19
    at new ManagedPromise (C:\ProgramData\npm\node_modules\selenium-webdriver\lib\promise.js:1017:7)
    at controlFlowExecute (C:\ProgramData\npm\node_modules\selenium-webdriver\testing\index.js:138:14)
    at TaskQueue.execute_ (C:\ProgramData\npm\node_modules\selenium-webdriver\lib\promise.js:2731:14)
    at TaskQueue.executeNext_ (C:\ProgramData\npm\node_modules\selenium-webdriver\lib\promise.js:2714:21)
    at asyncRun (C:\ProgramData\npm\node_modules\selenium-webdriver\lib\promise.js:2631:25)
    at C:\ProgramData\npm\node_modules\selenium-webdriver\lib\promise.js:639:7

As Sudharsan Selvaraj suggested, I used the following and it did the job nicely:正如 Sudharsan Selvaraj 建议的那样,我使用了以下内容并且它很好地完成了工作:

function clickByLinkTextScroll(text) {
    element = driver.findElement(By.linkText(text));
    driver.executeScript("arguments[0].scrollIntoView()", element);
    driver.sleep(300);
    element.click();
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM