I use Selenium to login into a system. I want to input verification code after browser.save_screenshot(r'img\login.png') finishes, but I must input the verification code first, I don't know why save_screenshot(r'img\login.png') should execute first.

How could I input verification code after save_screenshot()?

I used time.sleep(), it didn't work.

# -*- encoding: utf-8 -*-
'''
@File    :   login.py    
@Modify Time      @Author       @Desciption
------------      -------       -----------
2019/5/10 11:09   Jonas           None
'''

import time

from selenium.webdriver.chrome.options import Options
from selenium import webdriver

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')

path = 'E:\开发工具\chromedriver.exe'

browser = webdriver.Chrome(executable_path=path,chrome_options=chrome_options)

url = "http://10.153.195.92:8080/rdmp/rdmp/login.jsp"
browser.get(url)

browser.save_screenshot(r'img\login.png')

time.sleep(3)
# 查找input输入框
my_input = browser.find_element_by_id('username')
# 往框里面写入文字
my_input.send_keys("lijun2")

# 查找input输入框
my_input = browser.find_element_by_id('password')
# 往框里面写入文字
my_input.send_keys("1qaz!QAZ")

# 查找input输入框
my_input = browser.find_element_by_id('verificationcode')
# 往框里面写入文字
time.sleep(3)
code = input("please input verfication_code")
my_input.send_keys(code)
time.sleep(3)

# 让browser执行简单的js代码,模拟滚动到底部
js = 'document.body.scrollTop=10000'
browser.execute_script(js)

# 登录按钮
button = browser.find_element_by_xpath('//button')
button.click()

time.sleep(3)

browser.save_screenshot(r'img\login_result.png')

browser.switch_to.frame('mainFrame')

# 签出按钮
button = browser.find_element_by_xpath('/html/body/div[4]/div[1]/div[2]/ul/li[4]/p/a')
button.click()

time.sleep(3)

browser.save_screenshot(r'img\qianchu.png')

browser.quit()

first save_screenshot,then with images that i saved,input verification code

0 Answers