检测jquery-ui自动完成时是否与Selenium一起打开

时间:2015-10-01 18:00:22

标签: jquery python selenium jquery-autocomplete jquery-ui-autocomplete

我使用selenium / python对连接到本地运行的集线器的远程webdriver建模工作流。

工作流程包括在搜索自动填充中输入一些文本,然后等待它弹出结果。

我得到的最接近的是:

ul.ui-autocomplete

虽然我已经验证了使用DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:4444/wd/hub/session/fa48f475-6836-448e-a00a-e04808477fb0/element/28/value {"sessionId": "fa48f475-6836-448e-a00a-e04808477fb0", "id": "28", "value": ["S", "e", "a", "r", "c", "h", " ", "T", "e", "r", "m"]} DEBUG:selenium.webdriver.remote.remote_connection:Finished Request DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:4444/wd/hub/session/fa48f475-6836-448e-a00a-e04808477fb0/element {"using": "css selector", "sessionId": "fa48f475-6836-448e-a00a-e04808477fb0", "value": "ul.ui-autocomplete"} [01/Oct/2015 16:13:06]"GET /myapp/search/?q=Search%20Term HTTP/1.1" 200 136 DEBUG:selenium.webdriver.remote.remote_connection:Finished Request DEBUG:selenium.webdriver.remote.remote_connection:GET http://127.0.0.1:4444/wd/hub/session/fa48f475-6836-448e-a00a-e04808477fb0/element/29/displayed {"sessionId": "fa48f475-6836-448e-a00a-e04808477fb0", "id": "29"} DEBUG:selenium.webdriver.remote.remote_connection:Finished Request DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:4444/wd/hub/session/fa48f475-6836-448e-a00a-e04808477fb0/element {"using": "css selector", "sessionId": "fa48f475-6836-448e-a00a-e04808477fb0", "value": "ul.ui-autocomplete"} DEBUG:selenium.webdriver.remote.remote_connection:Finished Request DEBUG:selenium.webdriver.remote.remote_connection:GET http://127.0.0.1:4444/wd/hub/session/fa48f475-6836-448e-a00a-e04808477fb0/element/29/displayed {"sessionId": "fa48f475-6836-448e-a00a-e04808477fb0", "id": "29"} DEBUG:selenium.webdriver.remote.remote_connection:Finished Request DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:4444/wd/hub/session/fa48f475-6836-448e-a00a-e04808477fb0/element {"using": "css selector", "sessionId": "fa48f475-6836-448e-a00a-e04808477fb0", "value": "ul.ui-autocomplete"} DEBUG:selenium.webdriver.remote.remote_connection:Finished Request DEBUG:selenium.webdriver.remote.remote_connection:GET http://127.0.0.1:4444/wd/hub/session/fa48f475-6836-448e-a00a-e04808477fb0/element/29/displayed {"sessionId": "fa48f475-6836-448e-a00a-e04808477fb0", "id": "29"} DEBUG:selenium.webdriver.remote.remote_connection:Finished Request DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:4444/wd/hub/session/fa48f475-6836-448e-a00a-e04808477fb0/element {"using": "css selector", "sessionId": "fa48f475-6836-448e-a00a-e04808477fb0", "value": "ul.ui-autocomplete"} DEBUG:selenium.webdriver.remote.remote_connection:Finished Request DEBUG:selenium.webdriver.remote.remote_connection:GET http://127.0.0.1:4444/wd/hub/session/fa48f475-6836-448e-a00a-e04808477fb0/element/29/displayed {"sessionId": "fa48f475-6836-448e-a00a-e04808477fb0", "id": "29"} DEBUG:selenium.webdriver.remote.remote_connection:Finished Request Traceback (most recent call last): File "runtests.py", line 237, in <module> main() File "runtests.py", line 231, in main searchTest(browser) File "runtests.py", line 214, in createHandout Autocomplete didn't pop up File "/Users/beha/VEs/product-venv/lib/python2.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: Autocomplete didn't pop up 存在的浏览器的DOM检查器,但这让我超时了,我发现在Selenium放弃后,浏览器中的自动完成功能仍然可见。

我的方法总体上是错误的,还是我错过了API细节?

日志输出:

e

2 个答案:

答案 0 :(得分:0)

也许您可以直接检查元素的属性?

查看显示AutoComplete元素时更改哪个属性(可见性/已启用/显示),然后在WebDriverWait中使用该属性。

答案 1 :(得分:0)

https://stackoverflow.com/a/32921282/5398618的评论太长了,但是terle把我推向了大致正确的方向。这是怎么回事:

def selectFromAutocompleteField(browser, search_field, term):
    search_field.send_keys(term)
    WebDriverWait(browser, 2).until(
        firstElementWithCssAttribute('ul.ui-autocomplete', 'display', 'block')
    )
    search_field.send_keys(Keys.DOWN + Keys.RETURN)

def firstElementWithCssAttribute(css, attribute, content):
    def _withCssAttr(browser):
        for elem in browser.find_elements_by_css_selector(css):
            if elem and elem.value_of_css_property(attribute) == content:
                return elem
            else:
                continue
        return None

    return _withCssAttr

低效循环结构的原因是jQuery UI自动完成在使用期间创建了两个不同的ul.ui-autocomplete;第一个用法只会在第一次使用时使用,因此所有这些都必须进行轮询,并且可以在以后找到第一个可见的。

Selenium始终返回与selection =&gt;匹配的第一个项目在单个find_element只运行一次之后转到display:block,因此是额外的循环结构。

为什么Selenium的直接可见性检查不适用于Safari(即使只有一个ul存在一次)也是一个令人失望的谜 - 但输入上的clear()也是如此。< / p>