美文网首页
selenium模拟登录

selenium模拟登录

作者: king2king | 来源:发表于2019-08-19 12:13 被阅读0次

无界面Chrome headless浏览器

    # options = Options()
    # options.add_argument('--headless')
    # driver = webdriver.Chrome(options=options)

带界面Chrome浏览器

chrome_driver_path = '/Users/mjvcp/code/tools/chromedriver'
driver = webdriver.Chrome(executable_path=chrome_driver_path)

流程

确定待爬取界面 login_url

login_url = 'https://agent.ad-survey.com/'

请求login_url

driver.get(login_url)

打开url后,先尝试对验证码进行识别

  • 验证码识别是非常关键的一步,详情请查看验证码识别部分。
    此处使用打码平台对验证码进行获取
img = driver.find_element_by_id('validateCodeImg')
image_byte = img.screenshot_as_png
v_code = vcode(image_byte, '30500')
print(v_code)

输入信息,点击登录按钮

  • 在Chrome中打开开发者工具,mac下按住option+command+c选中要使用对元素,比如登录按钮,右键Copy--Copy Xpath。使用driver.find_element_by_xpath('')对元素进行定位。除此之外,还可以使用classname,id等对元素进行定位。
driver.find_element_by_id('login-userName').send_keys(username)
driver.find_element_by_id('login-userPass').send_keys(password)
driver.find_element_by_id('login-verifyCode').send_keys(v_code)
driver.find_element_by_id('login-submit').click()
time.sleep(2)

相关文章

网友评论

      本文标题:selenium模拟登录

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