【问题标题】:Not catching NoSuchElementException没有捕捉到 NoSuchElementException
【发布时间】:2019-08-02 08:47:33
【问题描述】:

我正在抓取一个网站的固定装置,然后使用另一个网站检查每个团队的表格。我遇到的问题是,并非所有团队都存在于表单网站上,我正在为那些在未找到 URL 页面上不存在 xPath 清除的团队获得 NoSuchElementException。我正在尝试捕获异常,但程序仍然中断。

我添加了一个 try catch,但它并没有解决我的问题,程序作为一个未找到的团队到达时立即中断。

 for(int i = 0; i < fixtures.getAwayTeams().size(); i++)
 {
     driver.navigate().to(FORMURL.concat( (fixtures.getAwayTeams().get(i)).replace( ' ', '+' )));          
     for (int j = 1; j < 11; j++) {
        String xPath = FORMXPATHONE.concat( String.valueOf( j ) ).concat(FORMXPATHTWO);
        try {
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xPath)));
            forms = driver.findElementsByXPath( xPath );
            } catch(NoSuchElementException | StaleElementReferenceException e) {
            awayTeamForm.add("No Form for Team");
            }
        for (WebElement languageElement : forms) {
            ArrayList <String> wld = new ArrayList<String>();
            wld.add( languageElement.getText() );
            String listedForm = String.join(",", wld );
            awayTeamForm.add(listedForm);
        }
    }
    }
 }  

原因:org.openqa.selenium.NoSuchElementException:无法定位元素://*[@id="results"]/table/tbody/tr[1]/td[6]

【问题讨论】:

  • 你可能听错了NoSuchElementException。您可能会发现默认的java.util.NoSuchElementException,而代码会抛出org.openqa.selenium.NoSuchElementException

标签: java selenium web-scraping


【解决方案1】:

您可以通过首先获取元素列表然后检查该列表的大小来检查该元素是否存在于页面上,如果它大于 0 则该元素存在,否则该元素不存在于页面上.这样,您也不需要捕获异常。

你可以这样做:

List<WebElement> elementList = driver.findElements(By.xpath("xPath"));
if(elementList.size()>0){
    // Element is present
}
else{
    // Element is not present
}

【讨论】:

  • 当我第一次这样做时wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xPath))); 当它看不到元素时我不会得到异常吗?
  • 不,它不起作用(您也面临着这种情况),因为当页面上不存在该元素时,它会引发空指针异常。这仅在元素出现在页面上但不可见时才有效。在您的情况下,页面上不存在该元素,因此您需要采用我在答案中提到的方法。
  • 虽然我不知道为什么它会丢失异常,但这个解决方案确实有效。谢谢。
【解决方案2】:

您的try-catch 声明看起来不错。这意味着,问题必须在其他地方。

您正在使用 selenium,这意味着有两个称为 NoSuchElementException 的异常可用。检查你的进口。您最有可能遇到的问题是,您确实导入 java.util.NoSuchElementException 而不是 org.openqa.selenium.NoSuchElementException

【讨论】:

  • 我已经检查并导入了 org.openqa.selenium.NoSuchElementException;而不是 java.util.NoSuchElementException
  • 那是战略。你能发布完整的 StackTrace 吗?也许那里有一些东西可以提供更好的提示。
  • 我可能看错了。看来我收到了由 NoSuchElementException 引起的 TimeoutException,也许我需要捕获 TimeoutException?线程“main”中的异常 org.openqa.selenium.TimeoutException:预期条件失败:等待 By.xpath 定位的元素的可见性://* 原因:org.openqa.selenium.NoSuchElementException:无法定位元素:// *[@id="results"]/table/tbody/tr[1]/td[6]
【解决方案3】:

尝试捕获整个循环体并进行调试,看看您是否捕获了正确的异常并在正确的行上。 你确定只有

forms = driver.findElementsByXPath( xPath );

可以引发异常吗?

【讨论】:

    【解决方案4】:

    试试这个:

    try {
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xPath)));
            forms = driver.findElementsByXPath( xPath );
            } catch(NoSuchElementException | StaleElementReferenceException | TimeoutException e) {
                awayTeamForm.add("No Form for Team");
        }
    

    【讨论】:

    • 线程“主”org.openqa.selenium.StaleElementReferenceException 中的异常: 的元素引用已过时;元素不再附加到 DOM,不在当前框架上下文中,或者文档已被刷新有关此错误的文档,请访问:seleniumhq.org/exceptions/stale_element_reference.html
    猜你喜欢
    • 2016-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    • 2020-10-19
    • 1970-01-01
    • 2020-07-23
    相关资源
    最近更新 更多