【问题标题】:How could I locate the xpath for below sequence我如何找到以下序列的 xpath
【发布时间】:2020-05-10 10:07:30
【问题描述】:

我如何找到以下动态变化 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]/button

给了我 3 个按钮来选择。

下面是包含按钮的 div 的完整 HTML

<div class="make_flex spaceBetween"><div class="faresLeftSection"><ul class="faresListing"><li class="make_flex"><span class="viewFaresIcon" style="background-image: url("https://imgak.mmtcdn.com/flights/assets/media/dt/common/multifare/tick-green.png?v=5");"></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("https://imgak.mmtcdn.com/flights/assets/media/dt/common/multifare/tick-green.png?v=5");"></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("https://imgak.mmtcdn.com/flights/assets/media/dt/common/multifare/tick-green.png?v=5");"></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("https://imgak.mmtcdn.com/flights/assets/media/dt/common/multifare/tick-green.png?v=5");"></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>

【问题讨论】:

  • 发布按钮的相关HTML?
  • 更新了问题并发布了 HTML

标签: java eclipse selenium webdriver


【解决方案1】:

点击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();

【讨论】:

  • 尝试了您提供的 xpath,但它仍然为我提供了立即预订的多个结果。它没有给我一个我试图点击的定位器元素。
  • 可以分享网址吗?
  • @vishalgupta :如果仍然有多个元素,请尝试使用索引。检查编辑部分。索引 1 是第一个元素。
  • 在这种情况下使用索引。此 URL 中的元素非常动态
【解决方案2】:

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

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

【讨论】:

  • 使用上面的语句给了我 3 个按钮来选择。我已经更新了问题并发布了完整的 HTML
猜你喜欢
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
  • 2014-10-05
  • 2022-11-23
  • 2022-11-28
  • 2019-06-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多