0

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 Answer 1

0
    **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++
    }

}

Sign up to request clarification or add additional context in comments.

2 Comments

I'm struggling to find the element itself. The desired element is what I can't find. What element loads after the table?
@rusty First check it in manually, then see how much time it takes to load the element and then scrape ur coding according.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.