Unable to get the correct value from the table using css selector in selenium



  • I am trying to get the value displayed against the Total using the below code:

    driver.get("https://www.cricbuzz.com/live-cricket-scorecard/20137/sl-vs-eng-4th-odi-england-tour-of-sri-lanka-2018");
    
    String textextras = driver.findElement(By.cssSelector("div[class='cb-col cb-col-100 cb-ltst-wgt-hdr']")).findElement(By.cssSelector("div:nth-last-child(3) div:nth-child(2)")).getText();
    System.out.println(textextras);
    
    String textsum = driver.findElement(By.cssSelector("div[class='cb-col cb-col-100 cb-ltst-wgt-hdr']")).findElement(By.cssSelector("div:nth-last-child(2) div:nth-child(2)")).getText();
    System.out.println(textsum);
    

    Output:

    19 Batsman R B 4s 6s SR

    Can anyone please help me why it is not working for textsum, but it is working fine for textextras variable ?



  • I am assuming you are trying to select the Total value from the first table. In short, the reason for the failure is that your css selector for table was not specific enough and it was selecting more than 1 table Element which causes undesired results.

    In your case, I would suggest you to do the following:

    1. Make sure the selector you are formulating is unique if you want to deal with a single element. The class you used to select the table was same for 4 tables and that is one of the reasons you got undesired results.

    div[class='cb-col cb-col-100 cb-ltst-wgt-hdr'] selects the 4 tables instead of selecting just the 1st one. Check the number of results for the selector in chrome. enter image description here

    1. Whenever possible, use id locators. They are faster and mostly unique. The reason I am telling you this is your table has a parent with an id locator and using that would have helped you with a unique locator for selecting the table.

    This is a unique selector for the first table - #innings_1 > div:first-child

    1. It is better to store the Element into a variable if you are going to use it more than once. You are using the table element twice. Here's an example.

      WebElement table = driver.findElement(By.cssSelector("div[class='cb-col cb-col-100 cb-ltst-wgt-hdr']"));
      String textextras = table.findElement(By.cssSelector("div:nth-last-child(3) div:nth-child(2)")).getText();
      System.out.println("textextras = "+textextras);
      String textsum = table.findElement(By.cssSelector("div:nth-last-child(2) div:nth-child(2)")).getText();
      
    2. Practise more with css locators.

    Here's the modified code. I just changed the selector for the table!

        WebElement table = driver.findElement(By.cssSelector("#innings_1 > div:first-child"));
        String textextras = table.findElement(By.cssSelector("div:nth-last-child(3) div:nth-child(2)")).getText();
        System.out.println("textextras = "+textextras);
        String textsum = table.findElement(By.cssSelector("div:nth-last-child(2) > div:nth-last-child(2)")).getText();
        System.out.println("textsum = "+textsum);
    

Log in to reply
 

Suggested Topics