如何在 HTML 单元格事件中搜索特定文本并单击同一单元格中的相应按钮?

How to search for specific text within HTML cell event and click corresponding button within same cell?

我的第一个 python 项目(也是我在 Whosebug 上的第一个 post 项目)需要一些帮助。感谢您的宝贵时间和帮助!

我正在尝试使用 selenium 和 chrome webdriver 在网站上自动进行定期预订。预订页面是一个时间表,每个单元格对应一个时间段和每个 cell/timeslot.

中的“预订”按钮

但是,这些按钮的 XPATH 是动态的。

例如//*[@id="root"]/div[4]/div/div[4]/div/div[3]/div[69]/div[3]/button

不同时隙的每个单元格和按钮具有相同的 html class 名称和类型。那么是否可以在每个单元格中搜索文本(例如 Class 1.10 7:30)并单击同一单元格中的相应按钮?

<div class="column">
  <div class="cell  even">
    <span class="d-block d-sm-none">Sat, 17th Jul</span>
    <span class="d-none d-sm-block">Saturday, 17th July</span></div>

  <div class="cell event " style="color: rgb(255, 255, 255); top: 8rem; height: 6rem;">
    <div>Class 1.1</div>
    <div>07:30</div>
    <div class="position-relative">
      <button class="btn btn-warning" type="button">Book</button>
    </div>
  </div>
    
  <div class="cell event " style="color: rgb(255, 255, 255); top: 16rem; height: 6rem;">
    <div>Class 1.2</div>
    <div>08:30</div>
    <div class="position-relative">
      <button class="btn btn-warning" type="button">Book</button>
      </div>
    </div>

是的,有可能。
如果你想匹配 2 个元素中的 2 个文本,如你提供的 HTML 所示,我猜这些文本作为参数传递给方法,它将是这样的:

btn_xpath = ("//div[./div[contains(text(),'Class $class')] and (./div[contains(text(),'$time')])]//button[text()='Book']",(class=class, time=time))
driver.find_element_by_xpath(btn_xpath).click()

这里classtime是从外部传递给select所需的class和时间的参数。
如果 selection 到那时只能是唯一的,它将是这样的:

btn_xpath = ("//div[./div[contains(text(),'$time')]]//button[text()='Book']",(time=time))
driver.find_element_by_xpath(btn_xpath).click()