点击滑动“ElementNotVisibleException”按钮selenium webdriver python

时间:2016-02-18 19:31:14

标签: python python-3.x selenium selenium-webdriver selenium-firefoxdriver

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会导致浏览器本身出错:

enter image description here

使用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分钟里,失败了,真的把风吹走了。如何点击这个阻塞按钮?谢谢

1 个答案:

答案 0 :(得分:1)

好的,问题实际上在你的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
相关问题