我如何找到以下序列的 xpath

How could I locate the xpath for below sequence

我如何找到以下动态更改 id 序列的 xpath

顺序是这样的

//*[@id="fli_list_item_5c7c4499-c9d1-45b7-b1df-d51c2f849140"]/div[3]/div[1]/div[2]/div[2]/button       -- button 1
//*[@id="fli_list_item_5c7c4499-c9d1-45b7-b1df-d51c2f849140"]/div[3]/div[2]/div[2]/div[2]/button       -- button 2    

//*[@id="fli_list_item_89b1488a-4b9a-4ed4-a8db-1040c88bae82"]/div[3]/div[1]/div[2]/div[2]/button       -- button 3
//*[@id="fli_list_item_89b1488a-4b9a-4ed4-a8db-1040c88bae82"]/div[3]/div[2]/div[2]/div[2]/button       -- button 4

//*[@id="fli_list_item_cdebfe1d-0f94-4a4b-a1f0-8024bb816464"]/div[3]/div[1]/div[2]/div[2]/button       -- button 5 
//*[@id="fli_list_item_cdebfe1d-0f94-4a4b-a1f0-8024bb816464"]/div[3]/div[2]/div[2]/div[2]/button       -- button 6

所以,使用

//*[包含(@id, "fli_list_item_")]/div[3]/div[1]/div[2]/div[2]/按钮

给我 3 个按钮 select。

下面是 div 的完整 HTML,其中包括按钮

<div class="make_flex spaceBetween"><div class="faresLeftSection"><ul class="faresListing"><li class="make_flex"><span class="viewFaresIcon" style="background-image: url(&quot;https://imgak.mmtcdn.com/flights/assets/media/dt/common/multifare/tick-green.png?v=5&quot;);"></span><span class="flexOne"><span class="service-text">Cabin Baggage 7 Kgs, Check-in Baggage 15 Kgs</span></span></li><li class="make_flex"><span class="viewFaresIcon" style="background-image: url(&quot;https://imgak.mmtcdn.com/flights/assets/media/dt/common/multifare/tick-green.png?v=5&quot;);"></span><span class="flexOne"><span class="service-text">Free seats available</span></span></li><li class="make_flex"><span class="viewFaresIcon" style="background-image: url(&quot;https://imgak.mmtcdn.com/flights/assets/media/dt/common/multifare/tick-green.png?v=5&quot;);"></span><span class="flexOne"><span class="service-text">Partially-refundable fare</span></span></li><li class="make_flex"><span class="viewFaresIcon" style="background-image: url(&quot;https://imgak.mmtcdn.com/flights/assets/media/dt/common/multifare/tick-green.png?v=5&quot;);"></span><span class="flexOne"><span class="service-text">Date change chargeable</span></span></li></ul></div><div class="faresRightSection"><div class="pull-left  make_relative price"><p><span class="actual-price">₹ 3,075</span></p></div><button class="btn fli_primary_btn text-uppercase"> Book Now </button></div></div>

您可以在 xpath 中使用 Contains。更多示例 here.

//*[contains(@id, "fli_list_item_")]/div[3]/div[1]/div[2]/div[2]/button

单击 Book Now 按钮会引发 WebDriverWait() 并等待 elementToBeClickable() 并遵循 xpath.

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(.,'Book Now')]"))).click();

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='faresRightSection']//button[contains(.,'Book Now')]"))).click();

编辑:如果您仍然获取多个元素,请使用索引。

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@class='faresRightSection']//button[contains(.,'Book Now')])[1]"))).click();