webdriver不是第二次按类名定位元素

时间:2016-01-10 17:17:46

标签: c# selenium-webdriver

我有一个'下一个'正在测试的Web应用程序的每个页面上的按钮。在表单的第一页上,我可以选择Next元素并单击,在第二页中,选择并单击相同的元素抛出错误:

- >错误:意外错误。元素在点(1007.5,244.14999389648438)处无法点击。其他元素将收到点击:

这是元素的HTML: div class =" submitarea" 输入类="后退按钮btn"类型="按钮"值="返回"> input class =" trigger-next-button btn btn-primary"类型="按钮"值="下一个"> 输入类="下一步按钮btn btn-primary"类型="按钮"值="下一个"> style =" display:none;" / DIV>

这是点击按钮的代码:

var nextBTN = testhelper.FireFoxBrowser.FindElement(By.ClassName("trigger-next-button"));
nextBTN.Click();

2 个答案:

答案 0 :(得分:0)

基于添加睡眠按预期工作的事实,听起来好像您的代码已经领先于WebDriver。您需要执行某种同步以使它们彼此串联。我会像这样利用陈旧性检查:

WebDriverWait wait = new WebDriverWait(driver, 60); // 60 is the time to wait, can be extended as needed. This is an 'up-to' wait and will not necessarily stop for 60 seconds.
    var nextBTN = testhelper.FireFoxBrowser.FindElement(By.ClassName("trigger-next-button"));
nextBTN.Click();
//Wait for that button to 'go away' before proceeding.
wait.until(ExpectedConditions.stalenessOf(nextBTN));
//Wait for another instance of the button to appear and be clickable before proceeding.
wait.until(ExpectedConditions.elementToBeClickable(By.ClassName("trigger-next-button")));
nextBTN = testhelper.FireFoxBrowser.FindElement(By.ClassName("trigger-next-button"));
nextBTN.Click();
wait.until(ExpectedConditions.stalenessOf(nextBTN));

有关等待和预期条件的更多信息,请参阅以下内容:

答案 1 :(得分:0)

当按钮在当前监视器视图中不可见或元素不存在或可单击时,通常会出现此错误。请尝试使用以下代码。

Webdriverwait wait = new webdriverwait(driver,60);

Actions builder = new Actions(driver);

Webelement element = wait.until(ExpectedConditions.elementtobeclickable(By.xpath('xpath'));

Builder.movetoelement(元素)。单击()执行();