gpt4 book ai didi

java - Webdriver/Xpath/Java : Crawl DOM to click on 'Element with No Unique Identifier'

转载 作者:行者123 更新时间:2023-12-01 12:57:12 25 4
gpt4 key购买 nike

我正在尝试从旅游网站的座位布局中查找并单击“可用”座位。挑战是,可用座位没有唯一标识符,而“已锁定”(已预订)座位则有“标题”形式的唯一标识符(请参阅 HTML)。我们如何让 WebDriver 跳过任何被阻塞的座位并在随机出现的座位布局上单击任何“可用”座位(图片)?

SeatLayout

HTML 显示 2 个被封锁的座位(L2、L4)和中间的一个可用座位(L3)的结构

    <div style="max-width:695px;">
<div class="GXXXXXXX" style="display: none;" aria-hidden="true">
<div class="GXXXXXXX">
<div class="GXXXXXXX"> </div>
<div class="GXXXXXXX">
<table>
<colgroup>
<tbody>
<tr>
<tr>
<td>
<td>
<td>
<td>
<td>
<td>
<td>
<td>
<td>
<td>
<td>
<td>
Blocked Seat
<div class="GDXXXXXX GDXXXXX0" style="overflow:hidden;position:static;margin: 0 5px 5px 0;" title="Seat Name: L2 | Fare: Rs. 300.0">L2</div>
</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td>
Available Seat
<div class="GXXXXXX GXXXXXX0" style="overflow:hidden;position:static;margin: 0 5px 5px 0;">L3</div>
</td>
</tr>
<tr>
<td>
<td>
<td>
<td>
<td>
<td>
<td>
<td>
<td>
<td>
<td>
<td>
Blocked Seat
<div class="GXXXXXX GXXXXXXX" style="overflow:hidden;position:static;margin: 0 5px 5px 0;" title="Seat Name: L4 | Fare: Rs. 300.0">L4</div>
</td>
</tr>
<tr>
</tbody>
</table>
</div>

最佳答案

这就是逻辑。查看DIV是否有title属性。如果没有座位也可以。根据您的需要更改逻辑。

List<WebElement> seats = driver.findElements(By.cssSelector("div.GXXXXXX.GXXXXXXX"));

for (WebElement seat : seats) {
if(seat.getAttribute("title") != null){
System.out.println("Seat is not available");
}else{
System.out.println("Seat is available");
seat.click(); // break the loop if you wish
}
}

关于java - Webdriver/Xpath/Java : Crawl DOM to click on 'Element with No Unique Identifier' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23777802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com