【问题标题】:Selecting a ClassName using Selenium, where there are multiple ClassNames with the same name使用 Selenium 选择一个 ClassName,其中有多个具有相同名称的 ClassName
【发布时间】:2020-05-24 03:03:54
【问题描述】:

我正在尝试在一个站点上选择一个按钮,其中多个元素的 className 相同,而每个元素的区别在于标题标签。

我现在有这个

    button = browser.find_element_by_class_name('start')
    button.click()

网页上有2个按钮,定义如下

<h4 class="start">1:48pm</h4>
<h4 class="start">3:54pm</h4>

我正在尝试根据开始类的标头标签中的时间指定要选择的脚本。有没有办法使用 Selenium 和 Python 来做到这一点?

【问题讨论】:

标签: python selenium


【解决方案1】:

我不确定您要达到的目标。如果它们有不同的 xpath,则可以从它们的父级遍历。如果您想单击一个或另一个按钮,您可以获取每个按钮的时间并进行比较(我以前从未尝试过,但我认为这是可能的)

【讨论】:

    【解决方案2】:

    使用 xpath 并选择正确元素的索引。注意我们正在使用 find_elements_... 而不是 find_element_...

    button = browser.find_elements_by_xpath('//h4[@class="start"]')[1].click()
    

    【讨论】:

      猜你喜欢
      • 2020-01-20
      • 2011-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-13
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      相关资源
      最近更新 更多