目前,我正在阅读一本学习selenium的书,无法正确地运行一些代码示例。下面的代码应该点击3块,但它只点击前2块。。。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.*;
public class MoveByOffSetAndClick {
public static void main(String... args){
WebDriver driver = new FirefoxDriver();
driver.get("file:///C:/Selectable.html");
WebElement one = driver.findElement(By.name("one"));
WebElement eleven = driver.findElement(By.name("eleven"));
WebElement six = driver.findElement(By.name("six"));
int borderWidth = 1;
Actions builder = new Actions(driver);
builder.moveByOffset(one.getLocation().getX() + borderWidth,
one.getLocation().getY() + borderWidth).click();
builder.build().perform();
builder.moveByOffset(six.getLocation().getX() + borderWidth,
six.getLocation().getY() + borderWidth).click();
builder.build().perform();
builder.moveByOffset(eleven.getLocation().getX() + borderWidth,
eleven.getLocation().getY() + borderWidth).click();
builder.build().perform();
driver.quit();
}
}有人知道为什么会发生这种事吗?
操作系统: Windows 7专业64位Java: 7u 71 x64 Eclipse:露娜服务第1版(4.4.1) 64位
这是它正在处理的HTML ..。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Selectable - Display as grid</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<style>
#feedback { font-size: 1.4em; }
#selectable .ui-selecting { background: #FECA40; }
#selectable .ui-selected { background: #F39814; color: white; }
#selectable { list-style-type: none; margin: 0; padding: 0; width: 450px; }
#selectable li { float: left; width: 100px; height: 80px; font-size: 4em; text-align: center; }
</style>
<script>
$(function() {
$( "#selectable" ).selectable();
});
</script>
</head>
<body>
<ol id="selectable">
<li class="ui-state-default" name="one">1</li>
<li class="ui-state-default" name="two">2</li>
<li class="ui-state-default" name="three">3</li>
<li class="ui-state-default" name="four">4</li>
<li class="ui-state-default" name="five">5</li>
<li class="ui-state-default" name="six">6</li>
<li class="ui-state-default" name="seven">7</li>
<li class="ui-state-default" name="eight">8</li>
<li class="ui-state-default" name="nine">9</li>
<li class="ui-state-default" name="ten">10</li>
<li class="ui-state-default" name="eleven">11</li>
<li class="ui-state-default" name="twelve">12</li>
</ol>
</body>
</html>发布于 2014-12-08 11:49:01
你正在使用的方法永远不能像你打算使用的那样工作。 moveByOffset说:
将鼠标从其当前位置(或0,0) 移动到给定的偏移量。
也就是说,它通过相对于当前鼠标位置的偏移量移动鼠标。在您的代码中,您使用相对于文档的坐标来调用它。这是第一次工作,因为如果还没有建立鼠标位置,那么初始鼠标坐标将被视为0, 0。它也是第二次工作,因为当您进行第二次调用时,鼠标就在您的第一个列表项中,因此仍然接近0, 0。当您单击时,鼠标单击的位置不是您的意思,但它仍然在six中,因此它不足以使您的代码遗漏six。但是,到第三次调用时,鼠标在调用之前在six中进行协调。这样,对moveByOffset的第三个调用将把鼠标移出eleven之外。
这是真正的错误或者没有错误。
通常,在单击内部元素时不需要担心边框。click()方法将自动将鼠标移动到您所关心的元素的中心点,因此通常无需调整鼠标位置即可正常工作。在特殊情况下,您可能需要担心补偿,但您还没有显示出需要担心它的情况。
发布于 2014-12-08 10:00:04
耶,苏,
是的-我用了下面的代码来绕过它。非常感谢您的投入!
WebDriver driver = new FirefoxDriver();
driver.get("file:///C:/Selectable.html");
WebElement one = driver.findElement(By.name("one"));
WebElement eleven = driver.findElement(By.name("eleven"));
WebElement six = driver.findElement(By.name("six"));
Actions builder = new Actions(driver);
builder.click(one).click(six).click(eleven);
builder.build().perform();发布于 2020-01-07 03:01:59
由于您已经定义了一个操作Actions builder = new Actions(driver);,并且您使用此操作多次调用moveByOffset,因此偏移量将累积,直到您清除或重置action对象,或为每次移动创建一个新的偏移量。
这是故意的。
下面是一个示例(在Python中):
//calculate offset
distance = get_distance()
#divide distance to small step
track = get_track(distance)
action = ActionChains(browser) #new action
action.click_and_hold(slide).perform()
#do slide
for step in track:
action.move_by_offset(xoffset=step, yoffset=0).perform()
print(step,slide.location['x'])
action.release().perform()我想它应该用distance移动一个滑块,但是它比distance移动得更多
程序输出:
1 23
1 25
2 29
2 35
2 43
3 54
3 68
3 85如你所见。左边是我期望移动的偏移量,右边是实际的协调。**对于每一次移动,它都是准确的之前的所有移动**
要解决这个问题很简单,只需重置操作,例如,您可以将action = ActionChains(browser) #new action放在for循环中。在您的情况下,您可以在第一次/第二次移动之后添加builder = new Actions(driver);。
https://stackoverflow.com/questions/27342519
复制相似问题