2

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.

emeraldgold07
  • 173
  • 3
  • 14
  • First you are using the implicit wait incorrectly (see [this](https://seleniumbyexamples.github.io/waitimplicit) ). Next when you logout, the login page might not be load. You can got to login page by clicking a link or driver.get (see [examples to nagivate](https://seleniumbyexamples.github.io/nav) ) – slackingslacker Aug 26 '20 at 09:02

1 Answers1

2

Issue is with function return type. As seeing structure of your code I can assume you are creating your WebDriver driver in some set up class and passing it around.

Now you need to understand when you don't add a return type to a function in python, by default it returns None. As a result in setup method is self.driver set to None.

Please add return driver statement to your setup method and it should work.

rahul rai
  • 2,260
  • 1
  • 7
  • 17
  • I already add `return driver` statement here: `#define variable driver def func1(): driver = webdriver.Chrome("C:/Users/sabrina/Downloads/chromedriver_win32/chromedriver.exe") driver.get("https://web.petbacker.com/") return driver driver = func1() driver.maximize_window() time.sleep(3)` but now got this error: `selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element` – emeraldgold07 Aug 27 '20 at 01:59
  • Now your original issue is resolved. NoSuchElementException we get if your xpath is not right , page is not loaded completely, element is in different frame etc. Need to see HTML DOM of your page. Also make return statement as last line of your function. Also please accept / upvote the answer as your original issue is resolved. Share DOM to enquire further. – rahul rai Aug 27 '20 at 02:14
  • Sorry but it's really my first time to hear HTML DOM. Is it the source code of the page? I tried out google but I don't think I know how to share the DOM with you. – emeraldgold07 Aug 27 '20 at 03:41
  • Yes. I am sorry for your trouble. I should have been more considerate. – rahul rai Aug 27 '20 at 03:54
  • But then for the first loop it is okay, it able to read data from excel. Supposed the xpath will not change after the first loop right? Do adding waits/sleep not solving the problem? – emeraldgold07 Aug 27 '20 at 03:54
  • Ideally it should not. But again can not tell exactly what is the reason of failure until can see what code you have written to identify the element and what is your element. – rahul rai Aug 27 '20 at 03:59