【问题标题】:Canceled page load listener because no navigation has been detected由于未检测到导航,已取消页面加载侦听器
【发布时间】:2019-09-16 16:10:37
【问题描述】:

无法单击按钮。火狐56.0版

WebDriver wd = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor) wd;
wd.get("https://www.seleniumeasy.com/test/basic-first-form-demo.html");

wd.findElement(By.xpath("//input[@id='sum1']")).sendKeys("5");
wd.findElement(By.xpath("//input[@id='sum2']")).sendKeys("10");
WebElement e1 = wd.findElement(By.xpath("//button[@class='btn btn-  
default']"));
js.executeScript("arguments[0].scrollIntoView();",e1 );
Thread.sleep(10000);
e1.click();

我想单击“获取总计”按钮,但显示“由于未检测到导航,已取消页面加载侦听器”消息。

【问题讨论】:

  • FWIW:FF 56 已经 2 岁以上。您应该更新 FF 和 geckodriver,看看问题是否仍然存在。

标签: java selenium xpath css-selectors webdriverwait


【解决方案1】:

诱导 WebDriverWaitelementToBeClickable() 并跟随 xpath

wd.get("https://www.seleniumeasy.com/test/basic-first-form-demo.html"); 
WebDriverWait wait = new WebDriverWait(wd, 10); 
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='sum1']"))).sendKeys("5");
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='sum2']"))).sendKeys("10");
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='btn btn-default'][text()='Get Total']"))).click();

【讨论】:

    【解决方案2】:

    要在文本为 Get Total 的元素上 click(),您必须为 elementToBeClickable() 诱导 WebDriverWait,您可以使用以下任一 Locator Strategies

    • cssSelector

      WebDriver wd = new FirefoxDriver();
      wd.get("https://www.seleniumeasy.com/test/basic-first-form-demo.html");
      new WebDriverWait(wd, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.form-control#sum1"))).sendKeys("5");
      wd.findElement(By.cssSelector("input.form-control#sum2")).sendKeys("5");
      wd.findElement(By.cssSelector("button.btn.btn-default[onclick^='return total']")).click();
      
    • xpath

      WebDriver wd = new FirefoxDriver();
      wd.get("https://www.seleniumeasy.com/test/basic-first-form-demo.html");
      new WebDriverWait(wd, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='form-control' and @id='sum1']"))).sendKeys("5");
      wd.findElement(By.xpath("//input[@class='form-control' and @id='sum2']")).sendKeys("5");
      wd.findElement(By.xpath("//button[@class='btn btn-default' and text()='Get Total']")).click();
      
    • 浏览器快照:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-09
      • 2019-06-04
      相关资源
      最近更新 更多