Selenium Web驱动程序自动化无法正常工作

时间:2016-07-14 20:59:32

标签: python selenium

所以我在网上发现了这个脚本,这意味着使用Selenium在网上蛮力网络等等,我认为接受它,修改它并尝试它是个好主意。这一次,我尝试创建一个机器人:

  1. 注册Twitter
  2. 转到twitter.com
  3. 发帖。
  4. 退出。
  5. 再次循环1,2,3,4
  6. 然而,当我运行脚本时,它只是弹出一个浏览器窗口而什么都不做。然后终端结束Python脚本,就像它正常工作一样,没有问题......

    代码(请注意,脚本可能看起来很奇怪我想要的东西,但那是因为我发现脚本是一个网络形式的暴力破解者在线,并决定根据我的需要修改它):

    #!/bin/python
    from mainLib import *
    from time import sleep
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    import simplejson as json
    import sys
    import optparse
    
    profile = webdriver.FirefoxProfile()
    profile.set_preference("general.useragent.override", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36")
    driver = "reserved"
    
    def userExists(username):
        try:
            driver.get("https://twitter.com/"+username)
            assert (("??" or "?Twitter / ?") not in driver.title)
        except AssertionError:
            print '\033[1;31mUser\033[1;m',
            print '\033[1;35m@%s\033[1;m' %username,
            print '\033[1;31mdoes not exist.\033[1;m',
            print '\033[1;33mTrying with the next Username...\033[1;m'
            print ' '
            return 1
        except:
            'uknown error'
    
    def login(user, password, delay):
        try:
            print '\033[1;33mCreating account with mail: \033[1;m' + '\033[1;35m' + password + '\033[1;m' '\033[1;33m ...\033[1;m'
            sleep(2)
            if driver.current_url == 'https://twitter.com':
                print '\033[1;33mPlease retry using a different IP Address (Proxy/VPN).\033[1;m'
                driver.get("https://twitter.com/signup")
            if driver.title == "Login on Twitter":
                driver.get("https://twitter.com/signup")
                sleep(3)
            elem = driver.find_element_by_id('full-name')
            elem.clear()
            elem.send_keys('john')
            elem = driver.find_element_by_id('email')
            elem.clear()
            elem.send_keys(password)
            elem = driver.find_element_by_id('password')
            elem.clear()
            elem.send_keys("twitter1")
            sleep(3)
            elem.send_keys(Keys.RETURN)
            sleep(delay + 3)
            if driver.title == "Twitter / Error":
                print '     \033[1;31mFailed!\033[1;m'
                driver.get("https://twitter.com/signup")
                sleep(3)
            if driver.title == "Login to Twitter":
                print '     \033[1;31mFailed!\033[1;m'
                driver.get("https://twitter.com/signup")
                sleep(3)
        #   if "This email is already registered." in driver.page_source:
        #       print '     \033[1;31mFailed!\033[1;m'
            if driver.current_url == 'https://twitter.com/account/access':
                print '     \033[1;31mFailed!\033[1;m'
                print ("")
                print '\033[1;33mPlease retry using a different IP Address (Proxy/VPN).\033[1;m'
                driver.close()
                sys.exit("")
    
            assert (("Enter your phone") not in driver.title)
    
        except AssertionError:
            print '     \033[1;32mSuccess!\033[1;m'
    #       print '\033[1;35mEmail: \033[1;m' + password
    #       print '\033[1;35mPassword: \033[1;m' + "twitter1"
    #       print("")
            try:
                f = open('CreatedAccounts.txt','a')
            except:
                f = open('CreatedAccounts.txt','w')
            f.write(password+'\n')
            f.close()
            driver.get("https://twitter.com")
            elem = driver.find_element_by_id('tweet-box-home-timeline')
            elem.clear()
            elem.send_keys('It worked!')
            elem = driver.find_element_by_xpath('//*[@id="timeline"]/div[2]/div/form/div[2]/div[2]/button')
            elem.send_keys(Keys.RETURN)
            time.sleep(5)
            driver.get("https://twitter.com/logout")
            sleep(5)
            elem = driver.find_element_by_css_selector("button.js-submit").click()
            sleep(5)
            driver.get("https://twitter.com/signup")
        #   driver.delete_all_cookies()
        #   return 1
    #   else:
    #       print '\033[1;33mPlease check your Internet Connection.\033[1;m'
    
    def dictionaryAttack(usernames,passwords,delay):
        if str(type(usernames)) == "<type 'list'>":
            for username in usernames:
                #if (userExists(username) == 1):
                #   continue
                driver.get("https://twitter.com/signup")
                sleep(delay)
                print("Creating Accounts...")
                print("")
                for password in passwords:
                    if (login(username,password,delay) == 1):
                        cj.clear()
                        break
    def main():
        parser = optparse.OptionParser()
        parser.add_option('-f', '--file', action="store", dest="userfile", help="File containing valid usernames (one per line)", default=False)
        parser.add_option('-d', '--dictionary', action="store", dest="dictionary", help="Text file containing passwords", default=False)
        parser.add_option('-u', '--username', action="store", dest="username", help="A valid username", default=False)
        parser.add_option('-t', '--time', action="store", dest="delay", help="Delay (in seconds) - use this option based on your Network Connection speed.", default=True)
        options, args = parser.parse_args()
    
        global driver
    
        if (options.delay is None):
            delay = 4
        else:
            delay = int(options.delay)
        print '\033[1;33mUsing\033[1;m',
        print '\033[1;35m%d second(s)\033[1;m' %delay,
        print '\033[1;33mof delay between login attempts.\033[1;m'
        print ' '
    
        if ( (options.userfile == False) and (options.username == False) ) :
            print 'You have to set an username or a userfile'
            exit()
        if ( (options.userfile != False) and (options.username != False) ) :
            print 'You can\'t set both options at once.. choose between username or userfile'
            exit()
        if (options.dictionary == False):
            print 'You have to set a valid path for the passwords dictionary.'
            exit()
    
        try:
            f = open(options.dictionary,'r')
            passwords = []
    
            while True:
                line = f.readline()
                if not line:
                    break
                passwords.append(line.strip('\n'))
            f.close()
        except:
            print 'Check the path to the dictionary and try again.'
            exit()
    
        if (options.userfile != False):
            try:
                f = open(options.userfile,'r')
                usernames = []
    
                while True:
                    line = f.readline()
                    if not line:
                        break
                    usernames.append(line.strip('\n'))
                f.close()
            except:
                print 'Check the path to the users file and try again.'
                exit()
    
                driver = webdriver.Firefox(profile)
                driver.implicitly_wait(30)
            dictionaryAttack(usernames,passwords,delay)
        else:
                driver = webdriver.Firefox(profile)
                driver.implicitly_wait(30)
            dictionaryAttack(options.username,passwords,delay)
    
        driver.close()
    if __name__ == '__main__':
        main()
    

1 个答案:

答案 0 :(得分:0)

请参阅以下链接。这可能是问题所在。

https://github.com/SeleniumHQ/selenium/issues/2257