Selenium2+python Automated 20-excel data parameterization "reprint"

Source: Internet
Author: User
Tags python script

Objective

Q: How does Python get a column value in Excel to Parameterize in a script recorded with selenium, such as how to parameterize a login user name and password?

A: You can use XLRD to read the contents of Excel for parameterization. Of course, in order to facilitate the detailed understanding of the small partners, the small series to introduce specific methods.

First, write the login use case:

STEP1: Visit http://www.effevo.com (play an advertising Effevo is Sogou self-developed project management system, completely free, very useful)
STEP2: Click Sign in at the top right corner of the page
STEP3: Login After entering user name and password
STEP4: Check if the avatar is present in the upper right corner
Implementation code:

Second, make Excel file:

Next, we will parameterize the username and password entered above so that the data is read from the Excel file. We name the Excel file Data.xlsx, which has two columns of data, the first column is Passname, and the second is password.

Third, the installation of the XLRD three-party library:

Method One: Pip online installation

1. Open cmd interface, enter command: Pip install xlrd

>>pip Install XLRD

Method Two: Download XLRD package Installation

Python reads an Excel file using a third-party library file xlrd, we download the HTTP://PYPI.PYTHON.ORG/PYPI/XLRD module installation on the Python website.
1. Download xlrd-0.9.4.tar.gz
2. Unzip the file. Because the file is compressed with the tar command, it is recommended that Windows users extract 7zip
3.cd to the directory of the extracted files, command line run setup.py file: Python setup.py install
4. After the installation is complete, we can import xlrd in the Python script.
Note: If the installation is not prompted, you can directly copy the installation files inside the XLRD directory to the D:\Python\Lib\site-packages directory can be used

Iv. parameterization of Excel data:

# Encoding:utf-8
From selenium import Webdriver
From selenium.common.exceptions import nosuchelementexception
From Selenium.webdriver.common.keys import keys
From Selenium.webdriver.support.ui import webdriverwait
Import unittest,time,xlrd
#import xdrlib, Sys
def open_excel (file= ' login.xlsx '):
Try
data = Xlrd.open_workbook (file)
Return data
Except Exception,e:
Print str (e)
#根据索引获取Excel表格中的数据 parameter: File:excel file path Colnameindex: Table header column name is the row, so by_index: Index of table
def excel_table_byindex (file= ' login.xlsx ', colnameindex=0,by_index=0):
data = Open_excel (file)
Table = Data.sheets () [By_index]
nrows = Table.nrows #行数
Colnames = Table.row_values (colnameindex) #某一行数据
List =[]
For rownum in range (1,nrows):
row = Table.row_values (rownum)
If row:
App = {}
For I in range (len (colnames)):
App[colnames[i]] = Row[i]
List.append (APP)
Return list


def login ():
Listdata = Excel_table_byindex ("C:\Users\hzy\Desktop\login.xlsx", 0)
if (Len (listdata) <= 0):
Assert 0, U "Excel data Exception"
For I in range (0, Len (listdata)):
Driver = Webdriver. Firefox ()
Driver.get ("https://workyun.com/")
#点击登录按钮
Driver.find_element_by_xpath ("html/body/div[2]/section[1]/div/div[2]/header/nav/div[3]/ul/li[1]/a"). Click ()
Time.sleep (1)
driver.find_element_by_id (' Passname '). Send_keys (listdata[i][' passname '])
driver.find_element_by_id (' password '). Send_keys (listdata[i][' password '])
driver.find_element_by_id ("//div[2]/div[2]/div/div[6]/input"). Click ()
Time.sleep (2)
Try
Elem = Driver.find_element_by_xpath ("//div[3]/div/div[1]/div[1]/a/img")
Except nosuchelementexception:
Assert 0, U "Login failed, can't find top left corner Avatar"
Driver.close ()
if __name__ = = ' __main__ ':
Login ()

Excel data Read method encapsulates reference Selenium2+python Automation 58-reading Excel data (XLRD)

If there are errors or unreasonable places, please give us a lot of advice!

Selenium2+python Automated 20-excel data parameterization "reprint"

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.