硒和硒Python(循环访问密码列表)

时间:2015-04-10 21:23:49

标签: python python-2.7 selenium selenium-webdriver

我有一个字符串列表,我希望使用selenium和Python 2.7x在密码字段中输入。以下是我的代码:

# album is a URL
def password_attempt(album):
    fp = webdriver.FirefoxProfile()
    fp.set_preference("network.proxy.type", 1)
    fp.set_preference("network.proxy.socks", "127.0.0.1")
    fp.set_preference("network.proxy.socks_port", 9150)
    fp.update_preferences()
    driver = webdriver.Firefox(firefox_profile=fp)
    driver.get(album)
    elem = driver.find_element_by_name("pwd")
    for password in password_list:
        elem.send_keys(password)
        elem.send_keys(Keys.RETURN)
        if album in driver.current_url:
            continue
        else:
            return driver.current_url

此代码无法运行,并且在运行时会返回错误。实质上,当输入列表中的密码时,如果密码不正确,页面只会使用与词干相同的URL和URL末尾的几个随机字符重新加载。然后,我想继续查看列表中的下一个密码并尝试一下。如果确实接受了密码,则该页面会重定向到新网页,其中包含的新网址不包含原始网址中的任何内容。

因此原始网址可能为http://www.myalbum.com,如果输入的密码不正确,则会重定向到http://www.myalbum.com/id=12344。但是,如果输入了正确的密码,则会重定向到http://www.abc124.com

Traceback (most recent call last):
  File "C:\Users\Dell\Desktop\code.py", line 203, in <module>
    thumbnail_identifier()
  File "C:\Users\Dell\Desktop\code.py", line 134, in thumbnail_identifier
    new_album = password_attempt(album)
  File "C:\Users\Dell\Desktop\code.py", line 186, in password_attempt
    elem.send_keys(Keys.RETURN)
  File "C:\Program Files (x86)\Python2.7.8\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\webelement.py", line 303, in send_keys
    self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': typing})
  File "C:\Program Files (x86)\Python2.7.8\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\webelement.py", line 385, in _execute
    return self._parent.execute(command, params)
  File "C:\Program Files (x86)\Python2.7.8\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 173, in execute
    self.error_handler.check_response(response)
  File "C:\Program Files (x86)\Python2.7.8\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 166, in check_response
    raise exception_class(message, screen, stacktrace)
StaleElementReferenceException: Message: 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:8329:1)
    at Utils.getElementAt (file:///c:/users/dell/appdata/local/temp/tmpbibio4/extensions/fxdriver@googlecode.com/components/command-processor.js:7922:10)
    at fxdriver.preconditions.visible (file:///c:/users/dell/appdata/local/temp/tmpbibio4/extensions/fxdriver@googlecode.com/components/command-processor.js:8957:11)
    at DelayedCommand.prototype.checkPreconditions_ (file:///c:/users/dell/appdata/local/temp/tmpbibio4/extensions/fxdriver@googlecode.com/components/command-processor.js:11618:15)
    at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/dell/appdata/local/temp/tmpbibio4/extensions/fxdriver@googlecode.com/components/command-processor.js:11635:11)
    at DelayedCommand.prototype.executeInternal_ (file:///c:/users/dell/appdata/local/temp/tmpbibio4/extensions/fxdriver@googlecode.com/components/command-processor.js:11640:7)
    at DelayedCommand.prototype.execute/< (file:///c:/users/dell/appdata/local/temp/tmpbibio4/extensions/fxdriver@googlecode.com/components/command-processor.js:11582:5)

任何帮助?

更新:我重新构建了我的代码,如下所示:

for password in password_list:
    elem = driver.find_element_by_name("pwd")
    elem.send_keys(password)
    elem.send_keys(Keys.RETURN)
    if album in driver.current_url:
        continue
    else:
        return driver.current_url

错误的一部分如下:

StaleElementReferenceException: Message: Element not found in the cache - perhaps the page has changed since it was looked up
Stacktrace:

0 个答案:

没有答案