使用selenium和python自动完成

使用selenium和python自动完成,python,selenium,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) 这可以正常工作,但自动完

我有一个带有搜索框的页面,如果我手动复制/粘贴一个单词或其一部分,搜索框将扩展为一个自动完成的框,几乎没有可能的搜索选项。如果我使用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)
这可以正常工作,但自动完成框不会出现在城市之后。发送\u键(一些\u值)。 我也尝试使用selenium的特殊键:

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)
selenium.common.exceptions.StaleElementReferenceException:Message:u'Element未在缓存中找到-可能页面在查找后已更改';堆栈跟踪:
在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)
在fxdriver.premissions.visible(file:///c:/users/jane/appdata/local/temp/tmpamb_no/extensions/fxdriver@googlecode.com/components/command_processor.js:8789:1)
在DelayedCommand.prototype.CheckPremissions\u(file:///c:/users/jane/appdata/local/temp/tmpamb_no/extensions/fxdriver@googlecode.com/components/command_processor.js:11438:1)
在DelayedCommand.prototype.executeInternal\uh处(file:///c:/users/jane/appdata/local/temp/tmpamb_no/extensions/fxdriver@googlecode.com/components/command_processor.js:11455:11)

在fxdriver.Timer.prototype.setTimeout/I上,我在维基百科上尝试了以下代码,效果很好:“driver=webdriver.Firefox()driver.get(')search\u elem=driver.find\u element\u by\u id('searchInput')search\u elem.clear()search\u elem.send\u keys('a')”谢谢评论,我尝试了与示例相同的代码,为我工作…事实上,我的代码也工作:)我以前在“some_value”字符串中携带“\n”,这导致了我遇到的问题。rstrip('some_value')修复了它。