点击 slippery "ElementNotVisibleException" 按钮 selenium webdriver python

clicking slippery "ElementNotVisibleException" button selenium webdriver python

https://gist.github.com/codyc4321/724f05aca8f6775e2fc1

您好,bitbucket 更改了登录页面,给我带来了麻烦。基于以下要点,使用 driver.click_button 导致:

ElementNotVisibleException at /bitbucket/create-repo
Message: Element is not currently visible and so may not be interacted with
Stacktrace:
    at fxdriver.preconditions.visible (file:///tmp/tmpSNzLIl/extensions/fxdriver@googlecode.com/components/command-processor.js:9981)
    at DelayedCommand.prototype.checkPreconditions_ (file:///tmp/tmpSNzLIl/extensions/fxdriver@googlecode.com/components/command-processor.js:12517)
    at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpSNzLIl/extensions/fxdriver@googlecode.com/components/command-processor.js:12534)
    at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpSNzLIl/extensions/fxdriver@googlecode.com/components/command-processor.js:12539)
    at DelayedCommand.prototype.execute/< (file:///tmp/tmpSNzLIl/extensions/fxdriver@googlecode.com/components/command-processor.js:12481)

使用driver.submit_form导致浏览器本身出错:

使用 driver.activate_hidden_element 导致:

ElementNotVisibleException at /bitbucket/create-repo
Message: Element is not currently visible and so may not be interacted with

activate_hidden_element 在最后 5 分钟里,失败真的让我失去了勇气。我怎样才能点击这个阻碍按钮?谢谢

好的,所以问题实际上出在您的 locate_element 方法中。

当您检查 xpath 时:"//button[normalize-space(text())='{text}']" 它成功找到了一个按钮,但它不是 您正在寻找的登录按钮。如果您使用 input xpath: "//input[@value='{text}']" 切换它,它会找到正确的 input 并成功登录。

您还应该删除 BitbucketDriver.login() 方法中的最后两行,因为 self.click_button(search_text="Log in") 行抛出 AttributeError.

您的 locate_element 方法应如下所示:

def locate_element(self, search_text, xpaths=None):
    if not xpaths:
        xpaths = [ "//input[@value='{text}']", "//button[normalize-space(text())='{text}']",
                  "//a[child::span[normalize-space(text())='{text}']]", "//a[normalize-space(text())='{text}']"]
    try:
        return self.driver.find_element_by_id(search_text)
    except:
        try:
            return self.driver.find_element_by_name(search_text)
        except:
            try:
                return self.driver.find_element_by_class_name(search_text)
            except:
                for path in xpaths:
                    try:
                        return self.driver.find_element_by_xpath(path.format(text=search_text))
                    except:
                        pass
    return None