I am attempting to modularize my script by breaking it down into various functions for easier reuse. However, when I do so, selenium seems to have trouble locating CSS selectors.
Here is my folder structure:
- Startup.py
- Webdriver folder
- Contains init.py
- and login.py
startup.py
from selenium import webdriver
from webtesting import login
def browser(usernamefield, passwordfield, loginbutton):
print 'Starting Firefox'
browser = webdriver.Firefox()
print 'Navigating to website address'
browser.get('website address')
login.loginsteps(usernamefield, passwordfield, loginbutton)
usernamefield = '#login-username'
passwordfield = 'input.input-default:nth-child(5)'
loginbutton = '.button'
if __name__=='__main__':
browser(usernamefield,passwordfield,loginbutton)
login.py
def loginsteps(usernamefield, passwordfield, loginbutton):
try:
userlogin = browser.find_element_by_css_selector(usernamefield)
print 'Found <%s> element with that class name!' % (userlogin)
except:
print 'Unable to find an element <%s> with that name.' % (usernamefield)
try:
userpass = browser.find_element_by_css_selector(passwordfield)
print 'Found <%s> element with that class name!' % (userpass)
except:
print 'Unable to find an element <%s> with that name.' % (passwordfield)
try:
logincss = browser.find_element_by_css_selector(loginbutton)
print 'Found <%s> element with that class name!' % (logincss)
except:
print 'Unable to click login.'
except:
print 'Unable to find login element.'
Although the browser starts up when I run the script, I encounter exceptions regarding the CSS selectors. This causes the second function to not execute correctly in the browser session.
Output in the terminal:
- Starting Firefox
- Navigating to website address
- Unable to find an element <#login-username> with that name.
- Unable to find an element with that name.
- Unable to find login element.
However, it works when I test it all within one function.
from selenium import webdriver
def browser(usernamefield, passwordfield, loginbutton):
print 'Starting Firefox'
browser = webdriver.Firefox()
print 'Navigating to website'
browser.get('website')
#login.loginsteps(usernamefield, passwordfield, loginbutton)
try:
userlogin = browser.find_element_by_css_selector(usernamefield)
print 'Found usernamefield element with that class name!'
except:
print 'Unable to find an element <%s> with that name.' % (usernamefield)
try:
userpass = browser.find_element_by_css_selector(passwordfield)
print 'Found userpass element with that class name!'
except:
print 'Unable to find an element <%s> with that name.' % (passwordfield)
try:
logincss = browser.find_element_by_css_selector(loginbutton)
print 'Found loginbutton element with that class name!'
except:
print 'Unable to find login element.'
usernamefield = '#login-username'
passwordfield = 'input.input-default:nth-child(5)'
loginbutton = '.button'
if __name__=='__main__':
startup.browser(usernamefield, passwordfield, loginbutton)
This method works, but it results in a messy script that cannot be easily broken down into separate functions for code reuse.
- Starting Firefox
- Navigating to website
- Found usernamefield element with that class name!
- Found userpass element with that class name!
- Found loginbutton element with that class name!
How can I make Python Selenium work with separate functions? Currently, I am forced to duplicate my script instead of being able to reuse code fragments.