【问题标题】:webdriver is not locating an element by class name the second timewebdriver 第二次没有按类名定位元素
【发布时间】:2016-04-14 23:41:49
【问题描述】:

我在被测 Web 应用程序的每个页面上都有一个“下一步”按钮。在表单的第一页上,我可以选择 Next 元素并单击,在第二页中,选择并单击相同的元素会引发错误:

-> 错误:意外错误。元素在点 (1007.5, 244.14999389648438) 处不可点击。其他元素会收到点击:

这是元素的 HTML: div class="提交区域" 输入 class="back-button btn" type="button" value ="Back"> 输入 class="trigger-next-button btn btn-primary" type="button" value ="Next"> 输入 class="next-button btn btn-primary" type="button" value ="Next"> style="display: none;" /div>

这是点击按钮的代码:

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

【问题讨论】:

  • 当我在这一步之前的步骤中添加以下代码时:System.Threading.Thread.Sleep(6000) 然后它就可以工作了。这是否意味着 webdriver 在页面完全加载之前尝试单击?如果是这样,webdriver 中是否有任何命令等待页面加载后再继续?

标签: c# selenium-webdriver


【解决方案1】:

基于添加睡眠按预期工作的事实,听起来您的代码似乎领先于 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));

有关等待和预期条件的更多信息,请参阅这些:

【讨论】:

    【解决方案2】:

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

    Webdriverwait 等待 = new webdriverwait(driver, 60);

    Actions builder = new Actions(driver);

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

    Builder.movetoelement(element).click().perform();

    【讨论】:

      猜你喜欢
      • 2016-12-30
      • 1970-01-01
      • 2020-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-18
      • 2020-05-19
      • 1970-01-01
      相关资源
      最近更新 更多