mercredi 26 août 2020

Selenium Python AttributeError: 'NoneType' object has no attribute 'send_keys'

I'm new in Selenium WebDriver, and currently using Python to do scripting. Now I want to apply parameter, which I applied data driven method using Excel. Basically only the first loop ok, it can read and write data for first row, but after that cannot.

Firstly, I create this in Python file (as new module):

import openpyxl

def getRowCount(file, sheetName):
    workbook = openpyxl.load_workbook(file)
    sheet = workbook.get_sheet_by_name(sheetName)
    return(sheet.max_row)

def getColumnCount(file, sheetName):
    workbook = openpyxl.load_workbook(file)
    sheet = workbook.get_sheet_by_name(sheetName)
    return(sheet.max_column)

def readData(file, sheetName, rownum,columno):
    workbook = openpyxl.load_workbook(file)
    sheet = workbook.get_sheet_by_name(sheetName)
    return sheet.cell(row=rownum, column=columno).value

def writeData(file,sheetName,rownum,columno,data):
    workbook = openpyxl.load_workbook(file)
    sheet = workbook.get_sheet_by_name(sheetName)
    sheet.cell(row=rownum, column=columno).value = data
    workbook.save(file)

Next, I write this code to include data driven test in my login and logout process:

path="<<my excel path>>"

rows=XLUtils.getRowCount(path, 'Sheet1')

for r in range(2,rows+1):
    email=XLUtils.readData(path,"Sheet1",r,1)
    password=XLUtils.readData(path,"Sheet1",r,2)

    driver.implicitly_wait(100)
    time.sleep(3)
    driver.find_element_by_xpath('<<my email field xpath element>>').send_keys(email)
    driver.implicitly_wait(100)
    time.sleep(3)
    driver.find_element_by_xpath('<<my password field xpath element>>').send_keys(password)
    driver.implicitly_wait(100)
    time.sleep(3)
    driver.find_element_by_xpath('<<my click login xpath element').click()
    driver.implicitly_wait(100)
    time.sleep(3)
    
    if driver.title=="<<to verify page title>>":
        print("test passed")
        XLUtils.writeData(path,"Sheet1",r,3,"test passed")
    else:
        print("test failed")
        XLUtils.writeData(path, "Sheet1", r, 3, "test failed")

    # Logout
    driver.find_element_by_css_selector("<<my logout element>>").click()
    time.sleep(2)
    driver.find_element_by_css_selector("<<my logout element>>").click()
    time.sleep(3)

I got the error AttributeError: 'NoneType' object has no attribute 'send_keys', so I thought including waits/sleep could solve the problem, but I still don't find a way to fix it. Can anyone help? Appreciate it. Thanks.

Aucun commentaire:

Enregistrer un commentaire