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

10,818

As Sudharsan Selvaraj suggested, I used the following and it did the job nicely:

function clickByLinkTextScroll(text) {
    element = driver.findElement(By.linkText(text));
    driver.executeScript("arguments[0].scrollIntoView()", element);
    driver.sleep(300);
    element.click();
};
Share:
10,818
Admin
Author by

Admin

Updated on June 22, 2022

Comments

  • Admin
    Admin almost 2 years

    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. 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:

    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