Alternative of isDisplayed() in Selenium java












-1















public void privateCohortCreation() {
if(webElements.newCohortElm.isDisplayed()) {
SeleniumUtils.click(getDriver(),webElements.createCohortSelectionFromMenu);
webElements.cohortname.sendKeys("private_cohort_test");
SeleniumUtils.click(getDriver(),webElements.createCohortButton);
}
else {
doApply();
}
}


I want that if the element is displayed then perform the task else call doApply() method. But this is giving an exception



"no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/app-root/div/app-container/app-indv301/app-global-filters/div/ul/li[3]/app-cohort/div/div/app-status/div"} (Session info: chrome=70.0.3538.77)"









share|improve this question




















  • 1





    You get the exception another place, where you use driver.findElement. You are unable to locate the element.

    – Guy
    Nov 13 '18 at 12:54











  • Inside SeleniumUtils class check implementation for click method which creates wrapper around findElement or findElements methods.

    – Amit Jain
    Nov 13 '18 at 13:12
















-1















public void privateCohortCreation() {
if(webElements.newCohortElm.isDisplayed()) {
SeleniumUtils.click(getDriver(),webElements.createCohortSelectionFromMenu);
webElements.cohortname.sendKeys("private_cohort_test");
SeleniumUtils.click(getDriver(),webElements.createCohortButton);
}
else {
doApply();
}
}


I want that if the element is displayed then perform the task else call doApply() method. But this is giving an exception



"no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/app-root/div/app-container/app-indv301/app-global-filters/div/ul/li[3]/app-cohort/div/div/app-status/div"} (Session info: chrome=70.0.3538.77)"









share|improve this question




















  • 1





    You get the exception another place, where you use driver.findElement. You are unable to locate the element.

    – Guy
    Nov 13 '18 at 12:54











  • Inside SeleniumUtils class check implementation for click method which creates wrapper around findElement or findElements methods.

    – Amit Jain
    Nov 13 '18 at 13:12














-1












-1








-1








public void privateCohortCreation() {
if(webElements.newCohortElm.isDisplayed()) {
SeleniumUtils.click(getDriver(),webElements.createCohortSelectionFromMenu);
webElements.cohortname.sendKeys("private_cohort_test");
SeleniumUtils.click(getDriver(),webElements.createCohortButton);
}
else {
doApply();
}
}


I want that if the element is displayed then perform the task else call doApply() method. But this is giving an exception



"no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/app-root/div/app-container/app-indv301/app-global-filters/div/ul/li[3]/app-cohort/div/div/app-status/div"} (Session info: chrome=70.0.3538.77)"









share|improve this question
















public void privateCohortCreation() {
if(webElements.newCohortElm.isDisplayed()) {
SeleniumUtils.click(getDriver(),webElements.createCohortSelectionFromMenu);
webElements.cohortname.sendKeys("private_cohort_test");
SeleniumUtils.click(getDriver(),webElements.createCohortButton);
}
else {
doApply();
}
}


I want that if the element is displayed then perform the task else call doApply() method. But this is giving an exception



"no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/app-root/div/app-container/app-indv301/app-global-filters/div/ul/li[3]/app-cohort/div/div/app-status/div"} (Session info: chrome=70.0.3538.77)"






java selenium






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 12:51









Guy

18.4k72149




18.4k72149










asked Nov 13 '18 at 12:49









Saroj PurbeySaroj Purbey

15310




15310








  • 1





    You get the exception another place, where you use driver.findElement. You are unable to locate the element.

    – Guy
    Nov 13 '18 at 12:54











  • Inside SeleniumUtils class check implementation for click method which creates wrapper around findElement or findElements methods.

    – Amit Jain
    Nov 13 '18 at 13:12














  • 1





    You get the exception another place, where you use driver.findElement. You are unable to locate the element.

    – Guy
    Nov 13 '18 at 12:54











  • Inside SeleniumUtils class check implementation for click method which creates wrapper around findElement or findElements methods.

    – Amit Jain
    Nov 13 '18 at 13:12








1




1





You get the exception another place, where you use driver.findElement. You are unable to locate the element.

– Guy
Nov 13 '18 at 12:54





You get the exception another place, where you use driver.findElement. You are unable to locate the element.

– Guy
Nov 13 '18 at 12:54













Inside SeleniumUtils class check implementation for click method which creates wrapper around findElement or findElements methods.

– Amit Jain
Nov 13 '18 at 13:12





Inside SeleniumUtils class check implementation for click method which creates wrapper around findElement or findElements methods.

– Amit Jain
Nov 13 '18 at 13:12












2 Answers
2






active

oldest

votes


















1














Try using try catch instead of if else.



 try {
if (webElements.newCohortElm.isDisplayed()) {
doApply();
}
}
catch (Exception e){
SeleniumUtils.click(getDriver(), webElements.createCohortSelectionFromMenu);
webElements.cohortname.sendKeys("private_cohort_test");
SeleniumUtils.click(getDriver(), webElements.createCohortButton);
}





share|improve this answer





















  • 1





    Yeah , I tried this and It worked for me. Thanks for Suggestion

    – Saroj Purbey
    Nov 14 '18 at 6:02



















2














You can use findElements() to check whether the element is on the webpage.



findElements() - returns empty list if there is no element with given locator
findElement() - throws NoSuchElementException if element is not on the page



Try below code:



List<WebElement> elements = driver.findElements(By.locator);
if(!elements.isEmpty()) {
if(elements.get(0).isDisplayed()) {
elements.get(0).click();
}
else {
// element not visible
}

}else{
// here mention code if element not present
}


Recommendation : Use relative xpath instead of absolute xpath. or try CSS selector instead.






share|improve this answer





















  • 1





    You've already scraped the page with the first driver.findElements() call... don't scrape the page two more times to check if it's displayed and click it. Store the collection from the first call in a variable and then use .get(0) to check if it's displayed and click it. See the update I made.

    – JeffC
    Nov 13 '18 at 15:00








  • 1





    @JeffC, Yep Agree and thanks for update.

    – NarendraR
    Nov 13 '18 at 15:09











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281384%2falternative-of-isdisplayed-in-selenium-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Try using try catch instead of if else.



 try {
if (webElements.newCohortElm.isDisplayed()) {
doApply();
}
}
catch (Exception e){
SeleniumUtils.click(getDriver(), webElements.createCohortSelectionFromMenu);
webElements.cohortname.sendKeys("private_cohort_test");
SeleniumUtils.click(getDriver(), webElements.createCohortButton);
}





share|improve this answer





















  • 1





    Yeah , I tried this and It worked for me. Thanks for Suggestion

    – Saroj Purbey
    Nov 14 '18 at 6:02
















1














Try using try catch instead of if else.



 try {
if (webElements.newCohortElm.isDisplayed()) {
doApply();
}
}
catch (Exception e){
SeleniumUtils.click(getDriver(), webElements.createCohortSelectionFromMenu);
webElements.cohortname.sendKeys("private_cohort_test");
SeleniumUtils.click(getDriver(), webElements.createCohortButton);
}





share|improve this answer





















  • 1





    Yeah , I tried this and It worked for me. Thanks for Suggestion

    – Saroj Purbey
    Nov 14 '18 at 6:02














1












1








1







Try using try catch instead of if else.



 try {
if (webElements.newCohortElm.isDisplayed()) {
doApply();
}
}
catch (Exception e){
SeleniumUtils.click(getDriver(), webElements.createCohortSelectionFromMenu);
webElements.cohortname.sendKeys("private_cohort_test");
SeleniumUtils.click(getDriver(), webElements.createCohortButton);
}





share|improve this answer















Try using try catch instead of if else.



 try {
if (webElements.newCohortElm.isDisplayed()) {
doApply();
}
}
catch (Exception e){
SeleniumUtils.click(getDriver(), webElements.createCohortSelectionFromMenu);
webElements.cohortname.sendKeys("private_cohort_test");
SeleniumUtils.click(getDriver(), webElements.createCohortButton);
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 5:46

























answered Nov 14 '18 at 4:34









Success ShresthaSuccess Shrestha

1408




1408








  • 1





    Yeah , I tried this and It worked for me. Thanks for Suggestion

    – Saroj Purbey
    Nov 14 '18 at 6:02














  • 1





    Yeah , I tried this and It worked for me. Thanks for Suggestion

    – Saroj Purbey
    Nov 14 '18 at 6:02








1




1





Yeah , I tried this and It worked for me. Thanks for Suggestion

– Saroj Purbey
Nov 14 '18 at 6:02





Yeah , I tried this and It worked for me. Thanks for Suggestion

– Saroj Purbey
Nov 14 '18 at 6:02













2














You can use findElements() to check whether the element is on the webpage.



findElements() - returns empty list if there is no element with given locator
findElement() - throws NoSuchElementException if element is not on the page



Try below code:



List<WebElement> elements = driver.findElements(By.locator);
if(!elements.isEmpty()) {
if(elements.get(0).isDisplayed()) {
elements.get(0).click();
}
else {
// element not visible
}

}else{
// here mention code if element not present
}


Recommendation : Use relative xpath instead of absolute xpath. or try CSS selector instead.






share|improve this answer





















  • 1





    You've already scraped the page with the first driver.findElements() call... don't scrape the page two more times to check if it's displayed and click it. Store the collection from the first call in a variable and then use .get(0) to check if it's displayed and click it. See the update I made.

    – JeffC
    Nov 13 '18 at 15:00








  • 1





    @JeffC, Yep Agree and thanks for update.

    – NarendraR
    Nov 13 '18 at 15:09
















2














You can use findElements() to check whether the element is on the webpage.



findElements() - returns empty list if there is no element with given locator
findElement() - throws NoSuchElementException if element is not on the page



Try below code:



List<WebElement> elements = driver.findElements(By.locator);
if(!elements.isEmpty()) {
if(elements.get(0).isDisplayed()) {
elements.get(0).click();
}
else {
// element not visible
}

}else{
// here mention code if element not present
}


Recommendation : Use relative xpath instead of absolute xpath. or try CSS selector instead.






share|improve this answer





















  • 1





    You've already scraped the page with the first driver.findElements() call... don't scrape the page two more times to check if it's displayed and click it. Store the collection from the first call in a variable and then use .get(0) to check if it's displayed and click it. See the update I made.

    – JeffC
    Nov 13 '18 at 15:00








  • 1





    @JeffC, Yep Agree and thanks for update.

    – NarendraR
    Nov 13 '18 at 15:09














2












2








2







You can use findElements() to check whether the element is on the webpage.



findElements() - returns empty list if there is no element with given locator
findElement() - throws NoSuchElementException if element is not on the page



Try below code:



List<WebElement> elements = driver.findElements(By.locator);
if(!elements.isEmpty()) {
if(elements.get(0).isDisplayed()) {
elements.get(0).click();
}
else {
// element not visible
}

}else{
// here mention code if element not present
}


Recommendation : Use relative xpath instead of absolute xpath. or try CSS selector instead.






share|improve this answer















You can use findElements() to check whether the element is on the webpage.



findElements() - returns empty list if there is no element with given locator
findElement() - throws NoSuchElementException if element is not on the page



Try below code:



List<WebElement> elements = driver.findElements(By.locator);
if(!elements.isEmpty()) {
if(elements.get(0).isDisplayed()) {
elements.get(0).click();
}
else {
// element not visible
}

}else{
// here mention code if element not present
}


Recommendation : Use relative xpath instead of absolute xpath. or try CSS selector instead.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 15:03









JeffC

12.2k41434




12.2k41434










answered Nov 13 '18 at 14:53









NarendraRNarendraR

3,81661743




3,81661743








  • 1





    You've already scraped the page with the first driver.findElements() call... don't scrape the page two more times to check if it's displayed and click it. Store the collection from the first call in a variable and then use .get(0) to check if it's displayed and click it. See the update I made.

    – JeffC
    Nov 13 '18 at 15:00








  • 1





    @JeffC, Yep Agree and thanks for update.

    – NarendraR
    Nov 13 '18 at 15:09














  • 1





    You've already scraped the page with the first driver.findElements() call... don't scrape the page two more times to check if it's displayed and click it. Store the collection from the first call in a variable and then use .get(0) to check if it's displayed and click it. See the update I made.

    – JeffC
    Nov 13 '18 at 15:00








  • 1





    @JeffC, Yep Agree and thanks for update.

    – NarendraR
    Nov 13 '18 at 15:09








1




1





You've already scraped the page with the first driver.findElements() call... don't scrape the page two more times to check if it's displayed and click it. Store the collection from the first call in a variable and then use .get(0) to check if it's displayed and click it. See the update I made.

– JeffC
Nov 13 '18 at 15:00







You've already scraped the page with the first driver.findElements() call... don't scrape the page two more times to check if it's displayed and click it. Store the collection from the first call in a variable and then use .get(0) to check if it's displayed and click it. See the update I made.

– JeffC
Nov 13 '18 at 15:00






1




1





@JeffC, Yep Agree and thanks for update.

– NarendraR
Nov 13 '18 at 15:09





@JeffC, Yep Agree and thanks for update.

– NarendraR
Nov 13 '18 at 15:09


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281384%2falternative-of-isdisplayed-in-selenium-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python