Processing (thirty-one) WebDriver verify the API code

Essays and records to facilitate their access to fellow travelers.

#------------------------------------------------I ------------------------------------------- dividing line is a shame

  Learning before selenium automation, it is best to learn HTML, CSS, JavaScript and other knowledge, help to understand the principles of operation and positioning elements. About python and selenium install their own search for other information,

Here do not introduced, all examples use python3.6 + selenium execution.

#------------------------------------------------I ------------------------------------------- dividing line is a shame

 

Processing a verification code

  For Web applications, most of the system when the user logs require the user to enter a verification code. Many types of code, alphanumeric, there are Chinese characters, and even user input is required to answer a question of arithmetic. For systems using codes can be effectively prevented using the method of spying robot guessing the password and increased safety to some extent.

  But for testers, whether it is performance testing or automated testing, it is a more difficult problem. In WebDriver it does not provide the appropriate way to handle the verification code, here I talk about several common approach to verification codes based on their experiences.

  1. remove codes

  This is the easiest way for developers, but the code of the relevant code commented out. If you are in a test environment, test personnel to do so can save a lot of trouble; but if the automated test script in the formal environment, then this approach will give the system has brought a certain amount of risk.

  2. Set universal codes

  The main problem is to remove the security code, in order to respond to security threats online system, you can not remove this code when modifying the program, but the program stay in a "back door" that set a "universal code." Whenever a user enters the "universal code", the program verification is approved, otherwise it determines that the user input verification code are correct.

  Design of universal codes is very simple manner, the user only needs to input information one more logic, by way of example below demonstrates.

 

from Random Import the randint 

# generate a random integer between a 1000-to 9999 
Verify the randint = (1000,9999 )
 Print (U " random number generated:% S " % Verify) 

Number = INPUT ( " Please enter the random number: " )
 Print (Number The) 
Number The = int (Number The) 

IF Number The == the Verify:
     Print ( " Login successful !! " )
 elif Number The == 5412 :
     Print ( " Login successful !! " )
 the else :
    Print ( " Verification code input is wrong! " )

  the randint () for generating a random number, the random number is set in the range of 1000 to 9999 between. Run the program codes are input correctly, universal codes and error codes, execution results are as follows:

 

 

 

 

 

 

 

  3. Verification Code Recognition

  For example, by Python-tesseract identified authentication code. Python-tesseract is an optical character recognition Tesseract OCR engine Python package type, can be any conventional read image file ( JPG , GIF , PNG , TIFF , etc.). However, the current code in the form of market a wide range, most of the code recognition technology, recognition rates are difficult to achieve 100% .

 

  4. Record cookie

 

  By conditions in the browser cookie you can bypass the login authentication code, which is a more interesting solution. For example, we check the option to "Remember Password" in the first time you log a site, when the next visit the site automatically logged in the. So naturally bypass the authentication code issues. The "remember password" function in fact recorded in the browser's cookie in. As already learned by WebDriver to operate the browser cookie , by add_cookie () Username Password method writes browser cookie , when once again visit the site, the server will directly read the browser's cookie login .

from selenium import webdriver
from time import sleep

# driver=webdriver.Firefox()
driver=webdriver.Chrome()
driver.get("https://baidu.com/")

#手动添加cookie

driver.add_cookie({'name':'BAIDUID','value':'92241303A3AC5BA1D9FD08FAA258A9BD:FG=1'})
driver.add_cookie({'name':'BDUSS','value':'ktFaVU3QXBSSUxLNjFxLWg0Qlg1NkJuWGtqU1h6alA2cnF5bzg0eEFxZFpTQ0piQVFBQUFBJCQAAAAAAAAAAAEAAAB~V4cC5~Pd0wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFm7-lpZu~paM'})

sleep(3)
driver.refresh()
sleep(3)

  The biggest problem with this approach is how the browser cookie find the user name and password corresponding key value, and enter the login information corresponding to wear. Can get_cookies () method to get all of the login cookie information, find the user name and password Key . Of course, a more direct way is to ask developer.

 

Guess you like

Origin www.cnblogs.com/lirongyang/p/11459817.html