How to skip NoSuchElementException in Selenium

Answered

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
How to skip NoSuchElementException in Selenium

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.

tilston1001 Train Asked on December 12, 2014 in Selenium WebDriver.
Add Comment
1 Answer(s)
Best answer

Hi Andy,

I am glad that you learned alot from the videos and applying all those techniques in your test.

The simplest workaround I can think of is , why not get the value of “Offer Of Payments” bar and if it is zero, then skip your further code to test Relational graph and contract details table. If it is not equal to zero, then do rest of your stuff.

I hope you get my point.

Regards,
Lakshay Sharma

xLov3rDns Professor Answered on December 12, 2014.

Hi Laksh, that’s good that you can see I’m putting the stuff I have learnt off you into practise :-)

Yep, what you are saying makes sense there. Do you have a working example I could refer to at all? I find it’s the best way for me to conceptualise a scenario

Cheers

Andy

on December 12, 2014.

That will be just a IF ELSE decision making
if(vlaueOFBar ==0){
//Do nothing
}else{
//Do your stuff
}

And I am sure you would be able to get the value of bar by using xpath of any locator.

Regards,
Lakshay Sharma

on December 12, 2014.

Great stuff, thank you Laksh :-)

on December 12, 2014.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.