xpath返回两个按钮

问题描述 投票:0回答:1

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>

enter image description here enter image description here

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

    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")

谢谢,Sameer

python selenium google-chrome xpath
1个回答
0
投票

感谢@JohnJordan的评论。页面上确实有两个按钮,其中一个是隐藏的。希望开发人员可以为所有元素使用唯一ID。

我删除了具有可见“Got It”按钮的模态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")
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.