1

I want to just print runs scored by all batsman during the cricket match in selenium using CSS selector. All rows have same classname and the runs are in 3rd row so I used CSS selector to select 3rd row only, but I am not able to print runs. Here is my code:

package SomeBasicAutomationPractice;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class tableGrid_Practice {

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "G:\\AutomationPractice\\src\\drivers\\chromedriver.exe");
        WebDriver driver= new ChromeDriver();
        driver.get("http://www.cricbuzz.com/live-cricket-scorecard/18970/pak-vs-sl-2nd-t20i-pakistan-v-sri-lanka-in-uae-2017");
        Thread.sleep(5000);
        WebElement table=driver.findElement(By.cssSelector("div[class='cb-col cb-col-100 cb-ltst-wgt-hdr']"));

        int count=table.findElements(By.cssSelector("div[classname='cb-col cb-col-100 cb-scrd-itms'] div:nth-child(3)")).size();
        System.out.println(count);
        for(int i=0;i<count;i++)
        {
            //table.findElements(By.cssSelector("div[classname='cb-col cb-col-100 cb-scrd-itms'] div:nth-child(3)")).get(i);
            System.out.println(table.findElements(By.cssSelector("div[classname='cb-col cb-col-100 cb-scrd-itms'] div:nth-child(3)")).get(i));

        }
    }
}
Amar Dev
  • 1,360
  • 2
  • 18
  • 38
Rohn Kerry
  • 155
  • 1
  • 4
  • 15

2 Answers2

1

To print the print runs scored by all batsman during the first innings within the website https://www.cricbuzz.com/live-cricket-scorecard/18970/pak-vs-sl-2nd-t20i-pakistan-v-sri-lanka-in-uae-2017 you need to induce WebDriverWait for the visibilityOfAllElementsLocatedBy() and you can use the following based Locator Strategy:

  • Code Block:

    driver.get("https://www.cricbuzz.com/live-cricket-scorecard/18970/pak-vs-sl-2nd-t20i-pakistan-v-sri-lanka-in-uae-2017");
    List<WebElement> runs = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div.ng-scope#innings_1>div div div.text-bold:nth-child(3)")));
      for(WebElement run:runs) { System.out.println(run.getText()); }
    driver.quit();
    
  • Console Output:

    R
    51
    19
    32
    1
    3
    1
    6
    0
    1
    2
    4
    

Alternative using Java 8 stream()

As an alternative you can use Java8 stream() and map() as follows:

  • Code Block:

    driver.get("https://www.cricbuzz.com/live-cricket-scorecard/18970/pak-vs-sl-2nd-t20i-pakistan-v-sri-lanka-in-uae-2017");
    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div.ng-scope#innings_1>div div div.text-bold:nth-child(3)"))).stream().map(element->element.getAttribute("innerHTML")).collect(Collectors.toList()));
    
  • Console Output:

    [R, 51, 19, 32, 1, 3, 1, 6, 0, 1, 2, 4]
    

Reference

You can find a relavant discussion in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Just to confirm the CSS selector path I have written is not correct? – Rohn Kerry Jun 01 '20 at 08:01
  • @RohnKerry I won't say the CSS selector path was incorrect rather I would like to avoid the `classname` values like `cb-col`, `cb-col-100` as I don't understand how they impacts the HTML. Where as I am sure about `classname` value `text-bold` makes the texts bolder. So I preferred them. – undetected Selenium Jun 01 '20 at 08:16
  • 1
    Ok, Thanks for confirming and your code is working fine. Thanks, – Rohn Kerry Jun 01 '20 at 08:43
0

In the above snippet, you are actually printing WebElement. findElements method returns List and get method on this list will return ith webelement. In order to print runs, you have to use getText() or getAttribute("attribute_name") on webelement, according to HTML structure of the page. You can modify your code something like this:

List<WebElement> list=table.findElements(By.cssSelector("div[classname='cb-col cb-col-100 cb-scrd-itms'] div:nth-child(3)"));
for(int i=0; i<list.size(); i++){
    System.out.println(list.get(i).getText());
}

Please modify your script accordingly and you will be able to print the runs.

Thanks!

Krishna Majgaonkar
  • 1,532
  • 14
  • 25