linhos
linhos

Reputation: 111

selenium move mouse don't work with low resolution

I want to use selenium to control chrome browser. In my work, i will move in a map, but selenium will work well ,when my screen resolution is 1920*1080, and it doesn't work when my screen resolution is 1366*768. the code is that:

ChromeDriver driver =(ChromeDriver)webDriver;
    WebElement map =driver.findElementsByClassName("ol-unselectable").get(0); // a MAP
    Actions actions =new Actions(driver);
    System.out.println(map.getSize());
    int x =map.getSize().width;
    int y =map.getSize().height;
    actions.moveToElement(map,0,0).perform();
    actions.moveByOffset(5,5).click().perform();
    actions.moveByOffset((int)(x*0.5),0).click().perform();
    actions.sendKeys(Keys.DOWN).sendKeys(Keys.DOWN).sendKeys(Keys.DOWN).perform();
    actions.sendKeys(Keys.DOWN).sendKeys(Keys.DOWN).sendKeys(Keys.DOWN).perform();
    actions.sendKeys(Keys.DOWN).sendKeys(Keys.DOWN).sendKeys(Keys.DOWN).perform();
    actions.moveToElement(map,(int)(x*0.5),(int)(y*0.8)).click().perform();
    actions.doubleClick().perform();

the code

actions.moveToElement(map,(int)(x*0.5),(int)(y*0.8)).click().perform();" 

doesn't work when I use a low resolution.

the MAP looks like this:enter image description here when my Screen Resolution is: 1920*1080, my Code running result looks like this:enter image description here
when I change my Screen Resolution to 1366*768, my Code running result looks like this:enter image description here So,we can find that,action can't move to the map element{0.5 width, 0.9 high}. How should I do?

Upvotes: 3

Views: 579

Answers (3)

Ankit Gupta
Ankit Gupta

Reputation: 786

Few points required sometimes with Actions class:

  1. Your element should be present on DOM, if it not how mouse hover would work? (To overcome it. Use scroll up or donw according to the position of the element)
  2. If more than one actions you are performing then use build().perform(); not only .perform().
  3. Sometimes we can use JavaScript focus method to focus on element before doing any actions using Actions class like below code:

                JavascriptExecutor js = (JavascriptExecutor) ts.getDriver();
                js.executeScript("arguments[0].focus();", we);
                actions.moveToElement(map,(int)(x*0.5),(int)(y*0.8)).click().build().perform();
    

Upvotes: 2

John Htet
John Htet

Reputation: 21

Try this,

actions builder = new Actions(driver);   

builder.moveToElement(map,(int)(x*0.5),(int)(y*0.8)).click().build().perform();

please try it. if i am not wrong, it will solve the issue.

Upvotes: 0

Anthony Gomez
Anthony Gomez

Reputation: 66

Try this :

actions builder = new Actions(driver);   
builder.moveToElement(map,(int)(x*0.5),(int)(y*0.8)).click().build().perform();

Maybe that will solve your problem Sorry i can not just comment under your question ...

Upvotes: 0

Related Questions