美文网首页Python
python+selenium实现无验证码用户名密码登陆

python+selenium实现无验证码用户名密码登陆

作者: sunland_0416 | 来源:发表于2020-10-22 08:53 被阅读0次

    环境:
    win10 x64
    python 3.6.5
    selenium-3.141.0
    chromedriver_win32_for_chrom85
    Chrome Version 85.0.4183.121 (Official Build) (32-bit)

    id,classname请根据实际情况获取自己的

    import time, sys
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    def main(argv):
        url = 'http://www.xxxxxxxx.com/'
        # 驱动路径,这里根据自己情况更改
        chrome_path = r'E:\Software\chromedriver.exe'
        # 让浏览器不显示自动化测试
        chrome_options = Options()
        # chrome_options.add_argument('--headless')
    
        driver = webdriver.Chrome(executable_path=chrome_path, chrome_options=chrome_options)
        driver.get(url)
    
        # 遮罩层处理
        shadow_mask= driver.find_elements_by_class_name('close')
        if len(shadow_mask) > 0:
            print("有遮罩层,长度为:"+str(len(shadow_mask)))
            shadow_mask[0].click()
            time.sleep(5)
    
        login_input = driver.find_element_by_id('tbUserAccount')
        passwd_input = driver.find_element_by_id('tbUserPwd')
        login_button = driver.find_element_by_class_name('login-btn')
    
        login_input.send_keys('username')
        passwd_input.send_keys('password')
        if login_button.is_displayed() and login_button.is_enabled():
            login_button.click()
        else:
            print("登陆按钮不可点击!")
            return
    
        time.sleep(2)
    
        cookie_list = driver.get_cookies()
        cookie = ""
        # 组装cookie字符串
        for item_cookie in cookie_list:
            item_str = item_cookie["name"] + "=" + item_cookie["value"] + "; "
            cookie += item_str
            # print(item_cookie)
        # 打印出来看一下
        print(cookie)
    
        for i in reversed(range(argv)):
            #这里的abc_main是自己要实现的功能函数
            abc_main.write_to_txt(cookie, i)
            time.sleep(1)
        driver.quit()
    
    
    if __name__ == "__main__":
        for i in sys.argv:
            print(i)
        if len(sys.argv)<2 :
            print("请输入页码数(2-46)")
        elif int(sys.argv[1])>1 and int(sys.argv[1])<47:
            main(int(sys.argv[1]))
        else:
            print("请输入合法页码数(2-46)")
    

    cmd 输入 python xxxx.py 2即可调用

    相关文章

      网友评论

        本文标题:python+selenium实现无验证码用户名密码登陆

        本文链接:https://www.haomeiwen.com/subject/haqumktx.html