Question Details

No question body available.

Tags

python selenium-webdriver pytest

Answers (1)

June 22, 2025 Score: 0 Rep: 26,575 Quality: Medium Completeness: 80%

Some feedback...

  1. What you are asking for is not a good design pattern. Instead investigate the page object model (POM) which uses one class to contain all locators and methods for a corresponding page. Each page will have it's own class, e.g. loginpage.py for the login page. So you would have two different page objects, each with a selectdate() or whatever method and each with their version of the field locator. This makes it very clear what code belongs to each page so if there are updates needed, you go to the page object that corresponds to the page that changed and update locators, methods, etc. It makes maintenance MUCH easier and readability is great. If you do this well, the code in your test will be almost human readable.

  2. wait.until(...) returns the found element(s). So,

    wait.until(...)
    element = self.driver.findelement(...)
    ...
    element.sendkeys(startdate)
    

    becomes

    wait.until(EC.visibilityofelementlocated(...).sendkeys(startdate)
    
  3. You've got code processing and formatting a date in the middle of selectstartdate(). Refactor that into a method, getdate() in another class, e.g. datetimehelper.py so that it's more reusable. Also, add a parameter to that method for the days ahead so it's even more flexible. You can even put a default value in as 1 for tomorrow.

    datetimehelper.py

    from datetime import date, timedelta

    def get
    date(daysahead: int = 1): return date.today() + timedelta(days=daysahead)
  4. 80s is a REALLY long wait... I would put the default at 10s and adjust as needed.

  5. Don't declare a variable in an except unless you are going to use it.

    except Exception as e:
    

    becomes

    except Exception:
    
  6. Also, don't catch Exception... that's WAY too broad. Catch only specific exceptions that you expect in "normal" circumstances. Otherwise you will run into some strange issue and won't be able to debug properly because you are eating ALL exceptions.

  7. Using time.sleep() is a bad practice also. Instead use WebDriverWait() and wait for a specific condition. I'm not saying don't ever use it... there are some instances when it's needed but always prefer WebDriverWait because it's a "smart wait". You may give it a timeout of 10s but if the element is immediately visible or clickable, the code will proceed. It only waits until the timeout if the condition isn't fulfilled which makes your script run faster and more predictably.

  8. Don't define locators as strings. Instead define as a By so that it's more usable and flexible.

    startdatefieldxpath="//input[@placeholder='Start date']"
    

    becomes

    startdatefield = (By.XPATH, "//input[@placeholder='Start date']")
    

Putting all this together, I would do it something like this...

test.py

from selenium import webdriver

from datetime
helper import getdate from somepage1 import SomePage1 from somepage2 import SomePage2

URL = "https://www.website.com" driver = webdriver.Chrome() driver.maximize
window() driver.get(URL)

somepage1 = SomePage1(driver) somepage1.selectstartdate(getdate())

do other stuff on page 1

navigate to new page

some
page2 = SomePage2(driver) somepage2.selectstartdate(getdate())

driver.quit()

datetimehelper.py

from datetime import date, timedelta

def get
date(daysahead: int = 1): return (date.today() + timedelta(days=daysahead)).strftime("%Y%m%d")

somepage1.py

import logging

from selenium import webdriver from selenium.common.exceptions import TimeoutException from selenium.webdriver.common.by import By from selenium.webdriver.common.action
chains import ActionChains from selenium.webdriver.common.keys import Keys from selenium.webdriver.support import expectedconditions as EC from selenium.webdriver.support.ui import WebDriverWait

class SomePage1: '''The name of the first page that uses select
startdate().''' def init(self, driver: webdriver): self.driver = driver self.logger = logging.getLogger() # Put into it's own class for consistency. self.startdatefield = (By.XPATH, "//input[@placeholder='Start date']")

def select
startdate(self, startdate): '''Selects the trip Start Date''' try: wait = WebDriverWait(self.driver, 10) wait.until(EC.visibilityofelementlocated(self.startdatefield)).sendkeys(startdate) # add appropriate wait here ActionChains(self.driver).sendkeys(Keys.ESCAPE).perform() self.logger.info("Selected start date")

except TimeoutException as e: self.logger.info("Could not select start date") self.logger.info("Exception message: %s", e) assert False

somepage2.py

import logging

from selenium import webdriver from selenium.common.exceptions import TimeoutException from selenium.webdriver.common.by import By from selenium.webdriver.common.action
chains import ActionChains from selenium.webdriver.common.keys import Keys from selenium.webdriver.support import expectedconditions as EC from selenium.webdriver.support.ui import WebDriverWait

class SomePage2: '''The name of the second page that uses select
startdate().''' def init(self, driver: webdriver): self.driver = driver self.logger = logging.getLogger() # Put into it's own class for consistency. self.selectdatefield = (By.XPATH, "//input[@placeholder='Choose a date']")

def select
startdate(self, startdate): '''Selects the trip Start Date''' try: wait = WebDriverWait(self.driver, 10) wait.until(EC.visibilityofelementlocated(self.selectdatefield)).sendkeys(startdate) # add appropriate wait here ActionChains(self.driver).sendkeys(Keys.ESCAPE).perform() self.logger.info("Selected start date")

except TimeoutException as e: self.logger.info("Could not select start date") raise