0

I am writing a selenium test in Javascript, where I want to move/hover cursor over a particular element. I tried bunch of things, but nothing seems to work -

1. driver.findElement(selector)
      .then(element => driver.actions().move({origin:element}).perform())
2. driver.findElement(selector)
      .then(element => driver.actions().moveToElement(element).perform())
3. driver.findElement(selector)
      .then(element => driver.actions({bridge: true}).moveMouse(element).perform())
4. driver.findElement(selector)
      .then(element => driver.actions({bridge: true}).move(element).clickAndHold().perform())
5. driver.findElement(selector)
      .then(element => driver.actions({bridge:true}).move({duration:5000,origin:element,x:0,y:0}).perform())

I am consuming 3.6.0 version of selenium-webdriver npm package.

Note - I am able to perform rest all driver actions like click, DoubleClick etc on that element, so essentially there are no issues in discovering that element. Just move action I am unable to perform - it is always giving TypeError: driver.actions(...).move is not a function

0
const {
    Builder,
    By,
    Key,
    until
} = require('selenium-webdriver');

let driver = new Builder()
    .forBrowser('chrome')
    .setChromeOptions( /* ... */ )
    .setFirefoxOptions( /* ... */ )
    .build();

driver.get("https://www.google.com/search?q=s&oq=s&aqs=chrome..69i57.210j0j15&sourceid=chrome&ie=UTF-8").then((test) => {


    driver.wait(until.elementLocated((By.css('div[role="dialog"] iframe')), 20000)).then((elemframe) => {

        driver.switchTo().frame(elemframe).then(
            
            driver.wait(until.elementLocated(By.xpath('(//*[contains(text(),"I agree")])[1]')), 20000).then((elem) => {

                driver.actions()
                .move({
                    x: 0,
                    y: 0,
                    origin: elem
                }).click().perform()

            }))
    })
});

Use latest npm release as the legacy action class doesn't have move in it .

https://www.selenium.dev/selenium/docs/api/javascript/module/selenium-webdriver/lib/actions_exports_LegacyActionSequence.html

else use mouseMove

2
  • Thanks much! This helps. – Jayarani garg Mar 31 at 13:00
  • Please accept the answer by clicking the tick sign thank you :) – PDHide Mar 31 at 13:07
0
const hoverLocation = driver.findElement(selector);
const actions = driver.actions({ bridge: true });
actions.move({ origin: hoverLocation }).perform();

Find your hover location, then you can perform the action over that location.

0

I have found the solution, We should change moveElement() to executeScript("arguments[0].scrollIntoView(true);", element) Here is the complete replacement:

const element = await driver.findElement(...)
await driver.executeScript("arguments[0].scrollIntoView(true);", element)
await sleep(500);

And sleep function:

function sleep(ms) {return new Promise(resolve => setTimeout(resolve, ms))}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.