skip to Main Content

I have just started learning Python and I’m diving in at the deep end with Selenium trying to learn from trial and error (so please go easy on me) – I have searched and searched, but I think being new to it all I am just searching for the wrong terms as I’ve had no success

I have moved from PHP, so I will try to explain using that as a crutch

Lets say we have a main script and a login script.
In the main script, we declare a "browser" object for example… The login script then happily uses this browser object to perform login. No issues, it works in PHP

//mainscript.php

require_once("login.php");
$browser = Object.browser;
login();


//login.php

function login(){
   //general login stuff goes here
   $browser.login();
}

So lets look at Python then… Doing the equivalent in Python

Browser is undefined.

How do I break up scripts in Python into smaller chunks by importing them like in the PHP example? Otherwise it will be one massive file impossible to read and write!?

I need to be able to break the scripts into parts so that functions such as "login" and others do not clog up the main script page, they can be called as required from the main script

//mainscript.py

import login
browser = webdriver.Firefox()
browser.get(webpage)
Login()


//login.py

def Login:
   //do some login stuff
   browser.find_element(...).click()

I have tried looking on the internet but I think I am searching for the wrong search terms as I am not yet familiar with Python

2

Answers


  1. When you import login, you are importing the module login.py. This imported module contains all of the variables, functions, classes etc. of login.py, but the import statement does not put these into global scope.

    Due to this, you need to do one of the following:

    1. Specify the attributes you wish to import into global scope:
    from login import Login
    Login()
    
    1. Use dot notation to access the attribute from the imported module:
    import login
    login.Login()
    
    1. Import all attributes from the module (not recommended):

    This is very similar to require_once in PHP, but considered poor practise as it can cause namespace pollution and name conflicts.

    from login import *
    Login()
    

    As an aside, function and variable names in python typically begin with a lower case letter by convention, with class names beginning with an upper case letter.

    Login or Signup to reply.
  2. My suggestion is to do some research on the page object model. The basic concept is each page is in a different class file and contains all locators for that page and methods for any actions the user would need to perform on that page. The advantage here is better organization, easier maintenance, code reuse, etc.

    Some resources to get you started:

    1. Selenium python docs, Page Objects
    2. Page Object Model (POM) In Selenium Python on lambdatest.com
    3. Page Object Model and Page Factory in Selenium Python on browserstack.com
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search