[英]Selenium moveByOffset doesn't do anything
我在Linux Xubuntu 13.10上使用Firefox 28.0运行最新的selenium 2.41
我试图让FirefoxDriver将鼠标移到页面上(在我的测试中,我使用了有线网页,它有很多悬停激活的菜单),但是moveByOffset
没有做任何明显的鼠标,根本:
package org.openqa.mytest;
import java.util.List;
import java.io.File;
import java.lang.*;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.*;
import org.apache.commons.io.FileUtils;
public class Example {
public static void main(String[] args) throws Exception {
// The Firefox driver supports javascript
FirefoxProfile profile = new FirefoxProfile();
profile.setEnableNativeEvents(true);
WebDriver driver = new FirefoxDriver(profile);
// Go to the Google Suggest home page
driver.get("http://www.wired.com");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// now save the screenshto to a file some place
FileUtils.copyFile(scrFile, new File("./screenshot.png"));
Actions builder = new Actions(driver);
Action moveM = builder.moveByOffset(40, 40).build();
moveM.perform();
Action click = builder.click().build();
click.perform();
//click.release();
Action moveM2 = builder.moveByOffset(50, 50).build();
moveM2.perform();
Action click2 = builder.click().build();
click2.perform();
//click2.release();
Action moveM3 = builder.moveByOffset(150, 540).build();
moveM3.perform();
for( int i=0; i < 1000; i++)
{
moveM = builder.moveByOffset(200, 200).build();
moveM.perform();
Thread.sleep(500);
moveM = builder.moveByOffset(-200, -200).build();
moveM.perform();
Thread.sleep(500);
}
//Action click3 = builder.click().build();
//click3.perform();
//click3.release();
scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// now save the screenshto to a file some place
FileUtils.copyFile(scrFile, new File("./screenshot2.png"));
driver.quit();
}
}
我期待鼠标移动到不同的元素上并触发所有悬停动作,但什么也没发生
类Actions
moveByOffset
方法已被破坏。 请参阅Selenium WebDriver Bug 3578
(错误在这个bug文档中描述了一些更多的行)。
项目成员(barancev)声称应该使用Selenium 2.42版修复此错误。
不过我在使用Firefox 33.0在openSUSE 12.3上运行的2.44版本中发现了同样的错误。 moveToElement
有效, moveToOffset
没有。
我一直在努力工作。
如果dragtarget不可见,似乎硒有问题,因此需要滚动。
无论如何,那是有效的(Java)代码。 请注意,我在没有参数的情况下调用“release()” - 可拖动元素和可拖动元素作为参数都不适用于我。 除了“moveToElement(dropable)”对我来说没有用,这就是我手动计算偏移量的原因。
public void dragAndDrop(WebElement dragable, WebElement dropable,
int dropableOffsetX, int dropableOffsetY) {
Actions builder = new Actions(driver);
int offsetX = dropable.getLocation().x + dropableOffsetX
- dragable.getLocation().x;
int offsetY = dropable.getLocation().y + dropableOffsetY
- dragable.getLocation().y;
builder.clickAndHold(dragable).moveByOffset(offsetX, offsetY).release()
.perform();
}
我也在努力解决这个问题,对我有用的解决方案是,我们必须在X或Y坐标上加1。
看起来(x,y)将我们带到了无法点击的元素的边缘
以下为我工作
WebElement elm = drv.findElement(By.name(str));
Point pt = elm.getLocation();
int NumberX=pt.getX();
int NumberY=pt.getY();
Actions act= new Actions(drv);
act.moveByOffset(NumberX+1, NumberY).click().build().perform();
你甚至可以尝试在y坐标上添加+1也可以
act.moveByOffset(NumberX+1, NumberY).click().build().perform();
请尝试使用moveToElement。 它应该工作。
Actions action = new Actions(webdriver);
WebElement we = webdriver.findElement(By.xpath("<XPATH HERE>"));
action.moveToElement(we).moveToElement(webdriver.findElement(By.xpath("/expression-here"))).click().build().perform();
我建议如果你的浏览器没有执行movetoelement并移动到偏移然后你已经为查找偏移量放置了错误的元素偏移你在Chrome中使用Cordinates插件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.