用selenium和python自动完成

时间:2014-07-21 16:23:26

标签: python selenium

我有一个带搜索框的页面,如果我手动复制/粘贴一个单词或只是其中的一部分,搜索框将扩展到一个自动完成框,其中包含几个可能的搜索选项。如果我用同样的方式通过python和selenium访问该页面:

city = driver.find_element_by_xpath("//input[@id='my_search_box_imput']")
city.click()
city.clear()
city.send_keys(some_value)
time.sleep(0.5)

这很好但是在city.send_keys(some_value)之后不会出现自动完成框。 我也尝试使用硒的特殊键:

from selenium.webdriver.common.keys import Keys

city = driver.find_element_by_xpath("//input[@id='my_search_box_imput']")
city.click()
city.clear()
city.send_keys(some_value)
city.send_keys(Keys.CONTROL, 'a') #highlight all in box
city.send_keys(Keys.CONTROL, 'c') #copy
city.send_keys(Keys.CONTROL, 'v') #paste    
city.send_keys(some_value)
time.sleep(0.5)

但在这种情况下,我收到了一个错误。

selenium.common.exceptions.StaleElementReferenceException: Message: u'Element not     found in the cache - perhaps the page has changed since it was looked up' ; Stacktrace:
        at fxdriver.cache.getElementAt (resource://fxdriver/modules/web_element_cache.js:8180:5)
        at Utils.getElementAt (file:///c:/users/jane/appdata/local/temp/tmpamb_no/extensions/fxdriver@googlecode.com/components/command_processor.js:7783:3)
        at fxdriver.preconditions.visible (file:///c:/users/jane/appdata/local/temp/tmpamb_no/extensions/fxdriver@googlecode.com/components/command_processor.js:8789:1)
        at DelayedCommand.prototype.checkPreconditions_ (file:///c:/users/jane/appdata/local/temp/tmpamb_no/extensions/fxdriver@googlecode.com/components/command_processor.js:11438:1)
        at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/jane/appdata/local/temp/tmpamb_no/extensions/fxdriver@googlecode.com/components/command_processor.js:11455:11)
        at fxdriver.Timer.prototype.setTimeout/<.notify (file:///c:/users/jane/appdata/local/temp/tmpamb_no/extensions/fxdriver@googlecode.com/components/command_processor.js:407:5)

到目前为止我找不到任何解决方案我想知道有蟒蛇/硒经验的人是否可以发表建议。谢谢!

我在Windows 7和firefox webdriver上使用pyhton 2.7。

0 个答案:

没有答案