从链接(https://www.cricbuzz.com/live-cricket-scorecard/22773/nz-vs-ind-1st-t20i-india-tour-of-new-zealand-2020),我想从印度各页打印数据。在WebElement(行)上使用XPath和cssSelector时,为什么会得到不同的结果?
使用XPath作为选择器的代码:
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());
}
}
}
输出:
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
使用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());
}
}
}
输出:
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
似乎您输入的定位器级别错误,请仔细检查