【问题标题】:xpath returns two buttonsxpath 返回两个按钮
【发布时间】:2019-07-30 02:37:57
【问题描述】:

Chrome 中的 xpath 表达式 $x("//button[text() = 'Got it']") 返回两个按钮元素,但页面中只有一个这样的按钮。

就数据而言,这两个元素看起来相同。 使用 selenium 和 python 有时可以单击数组的第二个元素,但多次返回 Element is not interactable 错误。

任何指针?

<button class="button slim-large" ng-click="ctrl.closeDialog()">Got it</button>

添加此代码来处理重复按钮的情况,其中一个可见而另一个不可见。

    button_clicked = False
    elems = self.find_all_by_xpath(locator="//button[contains(text(),'Got it')]")
    for elem in elems:
        if elem.is_displayed():
           elem.click()
           button_clicked = True

    if button_clicked == False:
       print("None of " + str(len(elems)) + " buttons are click-able")

谢谢, 萨米尔

【问题讨论】:

  • 请分享您的网址。
  • 页面中只有一个这样的按钮你是基于浏览器中页面的视觉外观,还是基于实际的html源代码?
  • @Winner 抱歉我不能分享网址
  • @NarendraR 这是标记。 <button class="button slim-large" ng-click="ctrl.closeDialog()">Got it</button>
  • 这个//button[text() = 'Got it'][@class='button slim-large']怎么样?

标签: python selenium google-chrome xpath


【解决方案1】:

感谢@JohnJordan 的评论。 页面上确实有两个按钮,一个被隐藏了。 Wish 开发者可以为所有元素使用唯一的 ID。

我删除了显示“得到它”按钮的模态 div 并再次运行 xpath,现在只返回了一个按钮!这意味着 DoM 中多了一个隐藏按钮。

修改后的代码来处理这种情况。

button_clicked = False
elems = self.find_all_by_xpath(locator="//button[contains(text(),'Got it')]")
for elem in elems:
    if elem.is_displayed():
       elem.click()
       button_clicked = True

if button_clicked == False:
   print("None of " + str(len(elems)) + " buttons are click-able")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 2022-10-23
    • 1970-01-01
    • 2018-01-18
    • 2016-09-08
    相关资源
    最近更新 更多