Reputation: 111
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?
Upvotes: 3
Views: 579
Reputation: 786
Few points required sometimes with Actions class:
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
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
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