Alternative of isDisplayed() in Selenium java
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
add a comment |
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
1
You get the exception another place, where you usedriver.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 aroundfindElement
orfindElements
methods.
– Amit Jain
Nov 13 '18 at 13:12
add a comment |
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
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
java selenium
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 usedriver.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 aroundfindElement
orfindElements
methods.
– Amit Jain
Nov 13 '18 at 13:12
add a comment |
1
You get the exception another place, where you usedriver.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 aroundfindElement
orfindElements
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
add a comment |
2 Answers
2
active
oldest
votes
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);
}
1
Yeah , I tried this and It worked for me. Thanks for Suggestion
– Saroj Purbey
Nov 14 '18 at 6:02
add a comment |
You can use findElements()
to check whether the element is on the webpage.
findElements()
- returns empty list if there is no element with given locatorfindElement()
- 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.
1
You've already scraped the page with the firstdriver.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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
}
1
Yeah , I tried this and It worked for me. Thanks for Suggestion
– Saroj Purbey
Nov 14 '18 at 6:02
add a comment |
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);
}
1
Yeah , I tried this and It worked for me. Thanks for Suggestion
– Saroj Purbey
Nov 14 '18 at 6:02
add a comment |
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);
}
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);
}
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
add a comment |
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
add a comment |
You can use findElements()
to check whether the element is on the webpage.
findElements()
- returns empty list if there is no element with given locatorfindElement()
- 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.
1
You've already scraped the page with the firstdriver.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
add a comment |
You can use findElements()
to check whether the element is on the webpage.
findElements()
- returns empty list if there is no element with given locatorfindElement()
- 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.
1
You've already scraped the page with the firstdriver.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
add a comment |
You can use findElements()
to check whether the element is on the webpage.
findElements()
- returns empty list if there is no element with given locatorfindElement()
- 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.
You can use findElements()
to check whether the element is on the webpage.
findElements()
- returns empty list if there is no element with given locatorfindElement()
- 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.
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 firstdriver.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
add a comment |
1
You've already scraped the page with the firstdriver.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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
orfindElements
methods.– Amit Jain
Nov 13 '18 at 13:12