尝试获取yatra.com网站中的结果链接数。列表中的文本显示适当的结果,但list的size()方法未显示正确的结果。
public class Yatra {
static {
System.setProperty("webdriver.chrome.driver",`enter code here` "./driver/chromedriver.exe");
}
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.yatra.com/");
driver.manage().window().maximize();
driver.findElement(By.id("BE_flight_origin_city")).sendKeys("BLR");
Thread.sleep(1000);
driver.findElement(By.id("BE_flight_origin_city")).sendKeys(Keys.ENTER);
driver.findElement(By.id("BE_flight_arrival_city")).sendKeys("DEL");
Thread.sleep(1000);
driver.findElement(By.id("BE_flight_arrival_city")).sendKeys(Keys.ENTER);
driver.findElement(By.id("BE_flight_depart_date")).click();
driver.findElement(By.id("a_2017_10_25")).click();
driver.findElement(By.id("BE_flight_flsearch_btn")).submit();
Thread.sleep(3000);
JavascriptExecutor js = (JavascriptExecutor) driver;
for (int i = 0; i < 10; i++) {
js.executeScript("window.scrollTo(0,document.body.scrollHeight)");
Thread.sleep(1000);
}
List<WebElement> myResult = driver.findElements(By.id("resultList_0"));
for (int j = 0; j < myResult.size(); j++) {
WebElement display = myResult.get(j);
String text = display.getText();
System.out.println(text);
}
System.out.println(myResult.size());
}
}
输出: Output file
答案 0 :(得分:1)
我可以看到用于捕获结果的元素定位器需要更改。
尝试使用以下xpath并希望这对您有帮助。
List<WebElement> myResult = driver.findElements(By.xpath("//div[@class='results spr-push']//div/ul"));
如果您需要任何澄清,请告诉我。
感谢。
答案 1 :(得分:1)
将您的定位器更改为一些不太具体的定位器。例如。改变:
driver.findElements(By.id("resultList_0"));
到
driver.findElements(By.xpath("//div[contains(@class, 'js-flightRow')]"));
答案 2 :(得分:0)
是对的。 List<WebElement> myResult
内有2个WebElements。
尝试:
List<WebElement> myResult = driver.findElements(By.id("resultList_0"));
System.out.println("BEFORE: " + myResult.size());
for (int j = 0; j < myResult.size(); j++) {
System.out.println("VALUE J: " + j );
WebElement display = myResult.get(j);
String text = display.getText();
System.out.println(text);
System.out.println("------------------------------");
}
System.out.println("AFTER: " + myResult.size());
输出结果为:
BEFORE: 2
VALUE J: 0
AIRLINE
DEPART
ARRIVE
DURATION
PRICE PER ADULT
------------------------------
VALUE J: 1
Jet Airways
9W - 818
05:40
08:15
02h 35m
Non Stop
Rs.3,367
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 836
07:00
09:40
02h 40m
Non Stop
Rs.3,367
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 808
15:15
17:55
02h 40m
Non Stop
Rs.3,367
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Spicejet
SG - 192
09:30
12:20
02h 50m
Non Stop
Rs.3,636
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Go Air
G8 - 116
08:10
10:50
02h 40m
Non Stop
Rs.3,650
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Indigo
6E - 3988
17:00
19:45
02h 45m
Non Stop
Rs.3,650
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Indigo
6E - 4065
12:20
15:15
02h 55m
Non Stop
Rs.3,650
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Go Air
G8 - 120
11:25
14:20
02h 55m
Non Stop
Rs.3,650
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 812
19:45
22:30
02h 45m
Non Stop
Rs.3,892
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Go Air
G8 - 118
20:45
23:30
02h 45m
Non Stop
Rs.4,100
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Spicejet
SG - 136
05:55
08:30
02h 35m
Non Stop
Rs.4,214
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Spicejet
SG - 150
00:20
03:05
02h 45m
Non Stop
Rs.4,214
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Spicejet
SG - 198
21:10
00:05
27 Oct
02h 55m
Non Stop
Rs.4,214
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Indigo
6E - 305
21:00
00:05
27 Oct
03h 05m
Non Stop
Rs.4,237
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 2314 / 840
16:10
21:05
04h 55m
1 Stop(s)
via Chennai
Rs.4,634
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 2314 / 822
16:10
09:15
27 Oct
17h 05m
1 Stop(s)
via Chennai
Rs.4,634
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 2314 / 740
16:10
16:10
27 Oct
24h 00m
1 Stop(s)
via Chennai
Rs.4,634
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 2314 / 7064
16:10
20:00
27 Oct
27h 50m
1 Stop(s)
via Chennai
Rs.4,634
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Air Asia
I5 - 722
23:50
02:35
27 Oct
02h 45m
Non Stop
Rs.4,809
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 2314 / 828
16:10
04:55
27 Oct
12h 45m
1 Stop(s)
via Chennai
Rs.4,844
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 7146 / 339
22:45
05:10
27 Oct
06h 25m
1 Stop(s)
via Mumbai
Rs.4,994
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 449 / 339
21:55
05:10
27 Oct
07h 15m
1 Stop(s)
via Mumbai
Rs.4,994
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 416 / 339
16:15
05:10
27 Oct
12h 55m
1 Stop(s)
via Mumbai
Rs.4,994
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 2314 / 831
16:10
22:15
06h 05m
1 Stop(s)
via Chennai
Rs.5,159
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 2314 / 736
16:10
13:20
27 Oct
21h 10m
1 Stop(s)
via Chennai
Rs.5,159
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 7051 / 7071
21:05
01:10
27 Oct
04h 05m
1 Stop(s)
via Pune
Rs.5,172
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 2847 / 372
13:35
22:15
08h 40m
1 Stop(s)
via Pune
Rs.5,172
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 2847 / 7071
13:35
01:10
27 Oct
11h 35m
1 Stop(s)
via Pune
Rs.5,172
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Indigo
6E - 246
13:00
15:40
02h 40m
Non Stop
Rs.5,220
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Indigo
6E - 475
04:30
07:15
02h 45m
Non Stop
Rs.5,220
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Indigo
6E - 367
08:35
11:20
02h 45m
Non Stop
Rs.5,220
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Indigo
6E - 978
14:40
17:25
02h 45m
Non Stop
Rs.5,220
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Indigo
6E - 289
21:55
00:55
27 Oct
03h 00m
Non Stop
Rs.5,220
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Indigo
6E - 943
09:55
13:00
03h 05m
Non Stop
Rs.5,220
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Air India
AI - 501
13:30
16:10
02h 40m
Non Stop
Rs.5,315
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Air India
AI - 804
06:10
08:55
02h 45m
Non Stop
Rs.5,315
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Air India
AI - 505
10:00
12:45
02h 45m
Non Stop
Rs.5,315
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Air India
AI - 173
21:10
23:55
02h 45m
Non Stop
Rs.5,315
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 478 / 331
07:00
12:10
05h 10m
1 Stop(s)
via Mumbai
Rs.5,362
Book Now
FLIGHT DETAILS
FREE MEALS
NON REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 7001 / 307
08:00
13:40
05h 40m
1 Stop(s)
via Mumbai
Rs.5,362
Book Now
FLIGHT DETAILS
FREE MEALS
NON REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 410 / 331
05:45
12:10
06h 25m
1 Stop(s)
via Mumbai
Rs.5,362
Book Now
FLIGHT DETAILS
FREE MEALS
NON REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 478 / 307
07:00
13:40
06h 40m
1 Stop(s)
via Mumbai
Rs.5,362
Book Now
FLIGHT DETAILS
FREE MEALS
NON REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 7001 / 351
08:00
15:15
07h 15m
1 Stop(s)
via Mumbai
Rs.5,362
Book Now
FLIGHT DETAILS
FREE MEALS
NON REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 410 / 307
05:45
13:40
07h 55m
1 Stop(s)
via Mumbai
Rs.5,362
Book Now
FLIGHT DETAILS
FREE MEALS
NON REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 478 / 351
07:00
15:15
08h 15m
1 Stop(s)
via Mumbai
Rs.5,362
Book Now
FLIGHT DETAILS
FREE MEALS
NON REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 7001 / 305
08:00
17:10
09h 10m
1 Stop(s)
via Mumbai
Rs.5,362
Book Now
FLIGHT DETAILS
FREE MEALS
NON REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 410 / 351
05:45
15:15
09h 30m
1 Stop(s)
via Mumbai
Rs.5,362
Book Now
FLIGHT DETAILS
FREE MEALS
NON REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 478 / 305
07:00
17:10
10h 10m
1 Stop(s)
via Mumbai
Rs.5,362
Book Now
FLIGHT DETAILS
FREE MEALS
NON REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 7146 / 301
22:45
09:05
27 Oct
10h 20m
1 Stop(s)
via Mumbai
Rs.5,362
Book Now
FLIGHT DETAILS
FREE MEALS
NON REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 7146 / 346
22:45
09:35
27 Oct
10h 50m
1 Stop(s)
via Mumbai
Rs.5,362
Book Now
FLIGHT DETAILS
FREE MEALS
NON REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 449 / 301
21:55
09:05
27 Oct
11h 10m
1 Stop(s)
via Mumbai
Rs.5,362
Book Now
FLIGHT DETAILS
FREE MEALS
NON REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 410 / 305
05:45
17:10
11h 25m
1 Stop(s)
via Mumbai
Rs.5,362
Book Now
FLIGHT DETAILS
FREE MEALS
NON REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 416 / 309
16:15
21:15
05h 00m
1 Stop(s)
via Mumbai
Rs.5,467
Book Now
FLIGHT DETAILS
FREE MEALS
NON REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 7133 / 353
12:50
19:05
06h 15m
1 Stop(s)
via Mumbai
Rs.5,467
Book Now
FLIGHT DETAILS
FREE MEALS
NON REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 416 / 361
16:15
23:10
06h 55m
1 Stop(s)
via Mumbai
Rs.5,467
Book Now
FLIGHT DETAILS
FREE MEALS
NON REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 7133 / 311
12:50
20:10
07h 20m
1 Stop(s)
via Mumbai
Rs.5,467
Book Now
FLIGHT DETAILS
FREE MEALS
NON REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 7133 / 309
12:50
21:15
08h 25m
1 Stop(s)
via Mumbai
Rs.5,467
Book Now
FLIGHT DETAILS
FREE MEALS
NON REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 7133 / 361
12:50
23:10
10h 20m
1 Stop(s)
via Mumbai
Rs.5,467
Book Now
FLIGHT DETAILS
FREE MEALS
NON REFUNDABLE
eCash Rs. 500
Indigo
6E - 484
23:00
01:45
27 Oct
02h 45m
Non Stop
Rs.5,500
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Vistara
UK - 868
06:50
09:25
02h 35m
Non Stop
Rs.5,646
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Vistara
UK - 812
09:55
12:35
02h 40m
Non Stop
Rs.5,646
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Vistara
UK - 816
12:15
15:05
02h 50m
Non Stop
Rs.5,646
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Indigo
6E - 855 / 564
18:45
23:40
04h 55m
1 Stop(s)
via Hyderabad
Rs.5,880
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Air India
AI - 564 / 43
11:50
23:55
12h 05m
1 Stop(s)
via Chennai
Rs.6,071
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Air India
AI - 564 / 440
11:50
08:55
27 Oct
21h 05m
1 Stop(s)
via Chennai
Rs.6,071
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Air India
AI - 564 / 143
11:50
11:35
27 Oct
23h 45m
1 Stop(s)
via Chennai
Rs.6,071
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Air India
AI - 564 / 430
11:50
12:55
27 Oct
25h 05m
1 Stop(s)
via Chennai
Rs.6,071
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Indigo
6E - 132
06:05
08:45
02h 40m
Non Stop
Rs.6,072
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 2736 / 822
22:25
09:15
27 Oct
10h 50m
1 Stop(s)
via Chennai
Rs.6,307
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 2312 / 822
20:35
09:15
27 Oct
12h 40m
1 Stop(s)
via Chennai
Rs.6,307
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Air India
AI - 503
17:00
19:45
02h 45m
Non Stop
Rs.6,365
Book Now
2 Seat(s) Left!
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 665 / 7143
02:50
08:35
05h 45m
1 Stop(s)
via Kolkata
Rs.6,758
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Indigo
6E - 602 / 846
05:30
10:00
04h 30m
1 Stop(s)
via Hyderabad
Rs.7,361
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Indigo
6E - 729 / 704
20:50
01:40
27 Oct
04h 50m
1 Stop(s)
via Chennai
Rs.7,471
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Air India
AI - 583 / 466
07:10
10:45
27 Oct
27h 35m
2 Stop(s)
via Trivandrum, Kochi
Rs.7,547
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Vistara
UK - 836
20:50
23:30
02h 40m
Non Stop
Rs.7,641
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Indigo
6E - 808
18:55
21:45
02h 50m
Non Stop
Rs.7,995
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 7120
21:35
00:15
27 Oct
02h 40m
Non Stop
Rs.8,286
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 973
09:20
12:05
02h 45m
Non Stop
Rs.8,286
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 820
11:25
14:10
02h 45m
Non Stop
Rs.8,286
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Indigo
6E - 783
18:00
20:45
02h 45m
Non Stop
Rs.8,657
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 816
17:45
20:30
02h 45m
Non Stop
Rs.8,811
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Indigo
6E - 186 / 966
07:30
12:05
04h 35m
1 Stop(s)
via Hyderabad
Rs.9,045
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Air India
AI - 564 / 539
11:50
19:50
08h 00m
1 Stop(s)
via Chennai
Rs.9,116
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Air India
AI - 604 / 677
08:50
15:10
06h 20m
1 Stop(s)
via Mumbai
Rs.9,536
Book Now
2 Seat(s) Left!
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Vistara
UK - 818
19:30
22:30
03h 00m
Non Stop
Rs.9,783
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Jet Airways
9W - 959 / 728
06:10
13:30
07h 20m
1 Stop(s)
via Patna
Rs.9,830
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Indigo
6E - 6228
07:05
09:45
02h 40m
Non Stop
Rs.10,305
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Indigo
6E - 6726
20:00
23:05
03h 05m
Non Stop
Rs.10,305
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Indigo
6E - 369 / 162
14:25
19:20
04h 55m
1 Stop(s)
via Ahmedabad
Rs.10,411
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Air India
AI - 640 / 809
06:45
12:15
05h 30m
1 Stop(s)
via Mumbai
Rs.10,586
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Air India
AI - 776 / 21
06:05
12:10
06h 05m
1 Stop(s)
via Kolkata
Rs.13,053
Book Now
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Air India
AI - 608 / 101
17:40
23:10
05h 30m
1 Stop(s)
via Mumbai
Rs.15,048
Book Now
4 Seat(s) Left!
FLIGHT DETAILS
FREE MEALS
REFUNDABLE
eCash Rs. 500
Go Air
G8 - 285
15:35
20:15
04h 40m
1 Stop(s)
via Goa
Rs.19,433
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Go Air Business
88 - 116
08:10
Business
10:50
02h 40m
Non Stop
Rs.19,535
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Go Air Business
88 - 118
20:45
Business
23:30
02h 45m
Non Stop
Rs.19,535
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
Go Air Business
88 - 120
11:25
Business
14:20
02h 55m
Non Stop
Rs.19,535
Book Now
FLIGHT DETAILS
REFUNDABLE
eCash Rs. 500
------------------------------
AFTER: 2