Forum Discussion

TanIlak's avatar
TanIlak
Contributor
2 years ago

Re: Unable to find an object

Hi,

I need to click a button multiple times in a page to load more contents untill the button gets disabled, if the button is disabled, I need to exit the process.

>> Sample code I have tried is:

var buttonObj =page.FindElement("xpath");

while(buttonObj.Visible==true)

{

 

buttonObj.click();

if(!page.FindElement("xpath"))

{

break;

}

 

}

 

After two iterations, the find element method that I highlighted fails and throws unable to find an object and gets logged in log file.

 

>> Any solution for my issues.

 

 

4 Replies

  • tvklovesu's avatar
    tvklovesu
    Frequent Contributor

    Visible will always first look for the object in the Dom and if the object doesn't exist then it throws the error even before checking the if condition. So instead of using Visible use FindElements('xpath') and in the if condition use Object.length=0. This will get the count and if the object doesn't exist then it will return 0

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    In this example,

    the button is disabled. However the property of Visible is True.

     

    You should get the button object, check that it exists. Then in your while loop, check the correct property value, when button is disabled. You don't need to perform the statement that you have highlighted.

     

    • tvklovesu's avatar
      tvklovesu
      Frequent Contributor

      Oh I missed that part that the button is disabled. So you need to check if the object.Enabled=true instead of Visible in your while loop and as rraghvani mentioned you don't need to use if condition. Again if the button is going to be enabled forever then your script will be stuck in that while loop. For that you need to use counter and if the counter reached certain number then you can exit out of the while loop and throw a log message that the button not disabled

  • Thanks for your response I will check with these concept