美文网首页我爱编程大数据 爬虫Python AI Sql
使用selenium模拟浏览器登录csdn牛刀小试

使用selenium模拟浏览器登录csdn牛刀小试

作者: BlueCat2016 | 来源:发表于2018-05-14 14:47 被阅读0次

    使用selenium模拟浏览器操作的方式登录CSDN,还好前几次操作没有遇到验证码问题,还算顺利。贴出源代码如下:

    from selenium import webdriver
    import time
    
    driver = webdriver.Firefox()
    try:
        driver.get("https://passport.csdn.net/account/login")
        # data = driver.title
        # print(data)
        # print(driver.page_source)
    
        log_by_account_link = driver.find_element_by_link_text('账号登录')
        print(log_by_account_link)
        log_by_account_link.click()
    
        username = driver.find_element_by_id('username')
        password = driver.find_element_by_id('password')
        log_button = driver.find_element_by_xpath("//form[@id='fm1']/input[@class='logging']")
    
        print(username)
        print(password)
        print(log_button)
        '''
        这一句sleep如果不加,则会提示Message: Element <input id="username" class="user-name" name="username" type="text"> could not be scrolled into view
        原因是页面还没加载完成,屏幕上没有发现username这个输入框。
        '''
        time.sleep(8)
        username.clear()
        username.send_keys('你的用户名')
        password.clear()
        password.send_keys('你的密码')
        time.sleep(5)
        log_button.click()
    
    except Exception as e:
        print(e)
    finally:
        driver.quit()
        print('OK')
    

    相关文章

      网友评论

        本文标题:使用selenium模拟浏览器登录csdn牛刀小试

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