Reputation: 15
From the link (https://www.cricbuzz.com/live-cricket-scorecard/22773/nz-vs-ind-1st-t20i-india-tour-of-new-zealand-2020) I want to print data from India innings. Why am I getting different results when I use XPath and cssSelector on WebElement (rows)?
Code using XPath as selector:
package basic;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Assignment8 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//div[@id='innings_2']/div[@class='cb-col cb-col-100 cb-ltst-wgt-hdr']
System.setProperty("webdriver.chrome.driver","C:\\Users\\AW5044309\\Desktop\\Selenium\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("https://www.cricbuzz.com/live-cricket-scorecard/22773/nz-vs-ind-1st-t20i-india-tour-of-new-zealand-2020");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement table = driver.findElement(By.xpath("//div[@id='innings_2']/div[@class='cb-col cb-col-100 cb-ltst-wgt-hdr']"));
List<WebElement> rows = table.findElements(By.xpath("//div[@class='cb-col cb-col-100 cb-scrd-itms']"));
for(WebElement a: rows) {
System.out.println(a.getText());
}
}
}
Output:
Guptill c Rohit b Shivam Dube30 19 4 1 157.89
Munro c Chahal b SN Thaku 59 42 6 2 140.48
Williamson (c) c Kohli b Chahal 51 26 4 4 196.15
de Grandhomme c Shivam Dube b Ravindra Jadeja 0 2 0 0 0.00
Ross Taylor
not out
54
27
3
3
200.00
Seifert (wk)
c Shreyas Iyer b Bumrah
1
2
0
0
50.00
Santner
not out
2
2
0
0
100.00
Extras
6
(b 0, lb 1, w 5, nb 0, p 0)
Total
203
(5 wkts, 20 Ov)
Did not Bat
Tickner, Southee, Ish Sodhi, Bennett
Rohit Sharma
c Ross Taylor b Santner
7
6
0
1
116.67
Lokesh Rahul (wk)
c Southee b Ish Sodhi
56
27
4
3
207.41
Virat Kohli (c)
c Guptill b Tickner
45
32
3
1
140.62
Shreyas Iyer
not out
58
29
5
3
200.00
Shivam Dube
c Southee b Ish Sodhi
13
9
1
1
144.44
Manish Pandey
not out
14
12
0
1
116.67
Extras
11
(b 0, lb 0, w 10, nb 1, p 0)
Total
204
(4 wkts, 19 Ov)
Did not Bat
Ravindra Jadeja, Shardul Thakur, Mohammed Shami, Yuzvendra Chahal, Jasprit Bumrah
CODE using cssSelector:
package basic;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Assignment8 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//div[@id='innings_2']/div[@class='cb-col cb-col-100 cb-ltst-wgt-hdr']
System.setProperty("webdriver.chrome.driver","C:\\Users\\AW5044309\\Desktop\\Selenium\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("https://www.cricbuzz.com/live-cricket-scorecard/22773/nz-vs-ind-1st-t20i-india-tour-of-new-zealand-2020");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement table = driver.findElement(By.xpath("//div[@id='innings_2']/div[@class='cb-col cb-col-100 cb-ltst-wgt-hdr']"));
List<WebElement> rows = table.findElements(By.cssSelector("div[class='cb-col cb-col-100 cb-scrd-itms']"));
for(WebElement a: rows) {
System.out.println(a.getText());
}
}
}
Output:
Rohit Sharma
c Ross Taylor b Santner
7
6
0
1
116.67
Lokesh Rahul (wk)
c Southee b Ish Sodhi
56
27
4
3
207.41
Virat Kohli (c)
c Guptill b Tickner
45
32
3
1
140.62
Shreyas Iyer
not out
58
29
5
3
200.00
Shivam Dube
c Southee b Ish Sodhi
13
9
1
1
144.44
Manish Pandey
not out
14
12
0
1
116.67
Extras
11
(b 0, lb 0, w 10, nb 1, p 0)
Total
204
(4 wkts, 19 Ov)
Did not Bat
Ravindra Jadeja, Shardul Thakur, Mohammed Shami, Yuzvendra Chahal, Jasprit Bumrah
Upvotes: 1
Views: 237
Reputation: 7708
While searching an element in another element using xpath it is important to add .
at the starting of xpath. Use below code
List<WebElement> rows = table.findElements(By.xpath(".//div[@class='cb-col cb-col-100 cb-scrd-itms']"));
The .
at the beginning means, that the current processing starts at the current node.
What if we don't use dot at the start what it signifies?
Then you'd select all element nodes with matching xpath //div[@class='cb-col cb-col-100 cb-scrd-itms']
in the whole document. (which is happening in current scenario)
Look out some info here and here
Upvotes: 1