Ask a Question

To find element is present or not

ḥari
Frequent Contributor

To find element is present or not

Hi
I want to verify the element is not available in the page.
If I use findelement it throws error and if I use waitelement it will return the object.
Using find element == page. Findelement ("xpath")
It throws error as unable to find the object
Using wait element it returns true but for my verification the element is not there so it need to fail.
Here what i need is I don't get any error in log file what method I can use here

Any other methods to use
8 REPLIES 8
DanNad
Contributor

I think WaitElement should be the right method for your case.
It will return a stub object that has only the Exists attribute and its set to false by default.

https://support.smartbear.com/testcomplete/docs/reference/test-objects/members/page/waitelement-meth...

 

If this still does return Exists == true then your object probably exists but is not visible.

What you can try is to check for exists and if its true you need to verify that it is not visible.
Or you have to specify your xpath so it will not find the element when its not visible.

ḥari
Frequent Contributor

@DanNadi have already tried waitelement it returns object only so the is not equal to

Refer below code 

Var element=page.WaitElement("xpath", 2000) 

If(element.Exists)   

Log. Error

Else

Log. Message
It returns objects is there and prints the message 

And also tried 

If(element.Visible==true) 

Log. Error

Else

Log. Message

Here also am getting message only

rraghvani
Champion Level 2

Using the Object Browser and enable "Show invisible objects",

rraghvani_0-1684481679699.png

Is your UI object appearing in the Object Browser? Your UI object may Exists, but it's property value Visibility is set to False. 

ḥari
Frequent Contributor

@rraghvani i checked in object browser in that invisible property invisible items are in false only for example I have section (0) to section (2) and footer (0) displays as true here I want to verify that section (3) is not added in that page I want to verify
rraghvani
Champion Level 2

Are you able to post an image similar to this, which shows the Object Browser and Properties for your UI object?

rraghvani_0-1684484175927.png

 

ḥari
Frequent Contributor

@rragvani it's not possible right now because I am sharing through mobile. I didn't see the option to share image
eykxas
Frequent Contributor

Hi.

 

To find if an element is present or not I use this script :

 

function get(page, value, presence = true, scroll = true) {
  const pageElement = utils.getPage(page);
  
  Options.Run.Timeout = 50;
  optimisation.stopLog();
  const elementHTML = pageElement.FindElement(value);
  optimisation.startLog();
  Options.Run.Timeout = 4000;
  
  
  if(presence && elementHTML.Exists) {
    elementHTML.scrollIntoView(scroll);
    Log.Message("On a trouvé l'élément :" + value);
    return true;
  }else if(!presence && !elementHTML.Exists) {
    Log.Message("On a pas trouvé: " + value);
    return true;
  }else{
    Log.Error(`On a ${presence ? "pas " : ""}trouvé: ${value}`);
    return false;
  }
}

 

 

stopLog and startLog are important here, because when TC didn't find the object, it return an error. But this error is expected if we want to not find the object. (in my case, I don't want to log this error).

ḥari
Frequent Contributor

@eykxas thank you for your effort
cancel
Showing results for 
Search instead for 
Did you mean: