Selenium+python Automation 21-txt Data parameterization

Source: Internet
Author: User
Tags readline

Preface

in 17 we talked about the parameterization of Excel data, someone asked the TXT data parameterization what to do, the following small series for you to take your TXT data parameterized explanation

First, Baidu search As an example, automatically search five times different keywords. The input data is different, which causes the output to change.

Test script:

1 #Coding=utf-82  fromSeleniumImportWebdriver3 Importunittest, time, OS4 classLogin (unittest. TestCase):5     defTest_login (self):6Source = open ("D:\\test\\txt.txt","R")7Values =Source.readlines ()8 source.close ()9         #Execution LoopTen          forHzyinchvalues: OneDriver=Webdriver. Firefox () ADriver.get ("http://www.baidu.com/") - Driver.maximize_window () -DRIVER.FIND_ELEMENT_BY_ID ("kw"). Send_keys (Hzy) theDRIVER.FIND_ELEMENT_BY_ID ("su"). Click () -Time.sleep (2) -Driver.close ()

TXT file:

The Open method opens the local txt.txt file as read-only (R), and the ReadLines method reads the entire file content line by row.

With a For loop, Hzy can get a row of data to the file each time, after locating the Baidu input box, the data is passed into Send_keys (Hzy). This is called by loop until all the contents of the file are read.

Second, login parameterization
Now according to the above ideas, the Automation script user, the name of the password parameterized, through the Python document we found that Python read the file: The entire file read, read-line, fixed byte read.

Did not find a good way to read two data at a time.

Create two files, each of which holds the user name password.

Test script:

1    #Coding=utf-82     fromSeleniumImportWebdriver3     fromSelenium.common.exceptionsImportnosuchelementexception4    Importunittest, time, OS5    classLogin (unittest. TestCase):6             defTest_login (self):7Source = open ("D:\\test\\un.txt","R")#User name File8UN = source.readline ()#Read user name9 source.close ()TenSource2 = open ("D:\\test\\pw.txt","R")#Password file OnePW = Source2.readline ()#Read Password A source2.close () -Driver=Webdriver. Firefox () -Driver.get ("http://www.baidu.com/") the Driver.maximize_window () -DRIVER.FIND_ELEMENT_BY_ID ("txtUserName"). Clear () -DRIVER.FIND_ELEMENT_BY_ID ("txtUserName"). Send_keys (un) -DRIVER.FIND_ELEMENT_BY_ID ("Txtpassword"). Clear () +DRIVER.FIND_ELEMENT_BY_ID ("Txtpassword"). Send_keys (PW) -DRIVER.FIND_ELEMENT_BY_ID ("Userlogin"). Click () +Time.sleep (2) A                 Try: att = Driver.find_element_by_xpath ("//form/div[4]/div/div[1]/div[1]/div/a/img") -                 exceptnosuchelementexception: -                        assert0, U"Login failed, unable to find upper left corner log" -Driver.close ()

Selenium+python Automation 21-txt Data parameterization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.