简体   繁体   中英

How to skip NoSuchElementException in Selenium

I have a scenario where I want to click on a bar from a graph('Responses'), which then displays a relational bar graph('Response "Offer Of Payment(Yes)"'). When I click on a bar from the relational bar graph it displays a list of contracts. This is highlighted in the screenshot below 截图

The issue I have is that the 'Responses' graph doesn't have a bar on the 'Offer Of Payment (Yes)' section. This is a valid test scenario, but what then happens is that the relational bar graph, on the right displays no data. Furthermore, when my selenium code then looks for a contract from the list, but can't find it,the test fails with a 'NoSuchElementException'. I understand why it does this but my question is....is there a way of me tweaking the code to cater for this kind of scenario so that if it can't find a relational graph (right hand side graph), and furthermore, a contract list in the table below, the code is able to skip past this?

I've attached details of my code below:

1) This method is attempting to open/close a contract from the list in the table. Can this be modified in some way so that if no contract is displayed (which is a valid scenario), the code can skip past the NoSuchElementException?

    public static void openCloseContractForReportWithBarChart(InternetExplorerDriver driver) throws InterruptedException {
    String winHandleBefore = driver.getWindowHandle();
    waitCommands.fluentWaitOnContractSelect(driver);

    for(String winHandle : driver.getWindowHandles()) {
       if(!winHandle.equals(winHandleBefore)){
                try{
                    Thread.sleep(4000);
                   }catch(InterruptedException e){
                   }
                driver.switchTo().window(winHandle);
                break;
            }
    }       
    driver.close();
    driver.switchTo().window(winHandleBefore);
}

2) You'll notice in the above method, I've referenced a 'FluentWaitOnContractSelect' method. Details of this are below:

    public static void fluentWaitOnContractSelect(InternetExplorerDriver driver)
{

    WebElement selectContractAfterWait = (new WebDriverWait(driver, 15))
            .until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='tabular-breakdown']/table/tbody/tr[1]/td[1]/a")));
    selectContractAfterWait.click();
}

Any help would be appreciated on this.

As far as I know , you can not skip element as do not know when exactly element will not found , so best way is to tell web driver to wait until next element found , Please user below for code for that :

 WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
 wait.until(ExpectedConditions.visibilityOfElementLocated(By.id[elementlocator]));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM