简体   繁体   中英

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: 在此输入图像描述 when my Screen Resolution is: 1920*1080 , my Code running result looks like this: 在此输入图像描述
when I change my Screen Resolution to 1366*768 , my Code running result looks like this: 在此输入图像描述 So,we can find that,action can't move to the map element{0.5 width, 0.9 high}. How should I do?

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(); 

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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