How to find the correct wait command for a dynamic table element?

51 Views Asked by At

The website is https://www.nseindia.com/get-quotes/derivatives?symbol=AARTIIND

Here is my code:

driver.get("https://www.nseindia.com/get-quotes/derivatives?symbol=AARTIIND");
try {
    wait.until(ExpectedConditions.elementToBeClickable(By.id("optionChain")));
} catch (Exception e) {
}
driver.findElement(By.id("optionChain")).click();
try {
    wait.until(ExpectedConditions.numberOfElementsToBeMoreThan(By.xpath("/html/body/div[10]/div[1]/div/section/div/div/div/div/div[1]/div[2]/div/div/div/div[2]/div[2]/div[2]/div/table[1]/tbody/tr"), 10));
} catch (Exception e) {
}
Thread.sleep(10000);
List<WebElement> table = driver.findElements(By.xpath("/html/body/div[10]/div[1]/div/section/div/div/div/div/div[1]/div[2]/div/div/div/div[2]/div[2]/div[2]/div/table[1]/tbody/tr"));
int optionChainSize = table.size();
        

I want to remove the Thread.sleep() command, but it invariably ends up with optionChainSize = 0.

I use "wait.until(ExpectedConditions..." to wait for the "Option Chain" element to load, and click it. Thereafter a table appears, but try as I might, I cannot get the program to wait for it to load, except a crude Thread.sleep command.

I have tried the bottom row that says "tot", the top right where it says "Underlying:", but they all just appear to load before the table. I have tried "numberOfElementsToBeMoreThan" and put the locator for the "tr" xpath, as follows:

try {
    wait.until(ExpectedConditions.numberOfElementsToBeMoreThan(By.xpath("/html/body/div[10]/div[1]/div/section/div/div/div/div/div[1]/div[2]/div/div/div/div[2]/div[2]/div[2]/div/table[1]/tbody/tr"), 10));
} catch (Exception e) {
        

Is there any way to do this right? I don't understand the "staleness" property, could that be of use?

1

There are 1 best solutions below

2
Bhairu On
    **Solution 1:**  
    
      Please try the below mechanism, actually, it will be polling every second and it stops the polling once you it get the desired element, it might work for you.
        
        FluentWait<WebDriver> wait = new FluentWait<> (driver)
          .withTimeout (10, TimeUnit.SECONDS)
          .pollingEvery (1, TimeUnit.SECONDS)
          .ignoring (NoSuchElementException.class);
        
        // Define the locator of the element to be clicked
        By locator = By.id("");
        
        // Wait until the element is clickable
        WebElement element = wait.until (ExpectedConditions.elementToBeClickable (locator));
        
        // Click on the element
        element.click();
    
    **Solutions 2:**
    
    here you can use the if-else condition to wait for until desired element.

//add loop 
for(i=0;i<=10)
{
    boolean WaitforThisElement=driver.findelement(By.xpath("pass the path of the waiting elememnt").isDisplayed();
    if(WaitforThisElement==true)
    {
    //write the code for the further activities.
    

//if the desired element is found it cannot move to the else block and will end the loop and come out from the loop and move to the next code.

    }
    else
    {
    //wait for the 10 seconds.
    Thread.wait(10000);
//Increase the loop count
i++
    }

}