Please visit link : https://www.cricbuzz.com/live-cricket-scorecard/22408/kxip-vs-dc-13th-match-indian-premier-league-2019
Here I want to print runs scored by individual player from table "Delhi Capitals Innings". I want to restrict the scope to table first and from then use method findElemets
to get desired output. I am using below code.
If replace
System.out.println(table.findElements(By.xpath("//div[@class='cb-col cb-col-8 text-right text-bold']")).get(i).getText());
with
System.out.println(table.findElements(By.xpath("//div[@id='innings_2']//div[@class='cb-col cb-col-100 cb-ltst-wgt-hdr'] //div[@class='cb-col cb-col-8 text-right text-bold']")).get(i).getText());
will get correct result. But I want to do it by limiting the scope of driver.
public class Runs{
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","C:\\ChromeDriver\\1\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.cricbuzz.com/live-cricket-scorecard/22408/kxip-vs-dc-13th-match-indian-premier-league-2019");
driver.manage().window().maximize();
WebElement table = driver.findElement(By.xpath("//div[@id='innings_2']//div[@class='cb-col cb-col-100 cb-ltst-wgt-hdr']"));
int rowCount = table.findElements(By.xpath("//div[@class='cb-col cb-col-8 text-right text-bold']")).size();
for(int i=0;i<rowCount;i++) {
System.out.println(table.findElements(By.xpath("//div[@class='cb-col cb-col-8 text-right text-bold']")).get(i).getText());
}
}
}
I am getting below output:
R
15
20
6
39
43
29
1
3
1
0
0
2
3
0
2
0
0
R
0
30
28
39
38
0
2
0
0
4
0
2
4
2
1
0
0
It is printing all the elements matching the provided xpath in entire web page. It should print only one present under table "Delhi Capitals Innings". What I am missing in this ?