2

I'm trying to test a website with selenium where I need to log into the website at the very first step. So instead of writing steps for login in every test, I defined a login function which I can call at the beginning of every test. I can then call the login function at the start and immediately continue with the actual test. However, after the login function call has ended and selenium is back in the original test, selenium doesn't seem to recognize basic methods like title, find_element etc and returns attribute errors. The following is the code I have for the main test

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common import web_client_login
from selenium.webdriver.common.web_client_login import login
import unittest
import time

login()

wait = WebDriverWait(webdriver, 5)
print(dir(webdriver))
print(webdriver.title)

The login function is defined as follows:

import unittest
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait

chrome_driver_path = 'C:/webdriver/chromedriver.exe'
driver = webdriver.Chrome(chrome_driver_path)
driver.implicitly_wait(5)

url = "https://xxx-xxxx.de"
driver.get(url)

driver.maximize_window()
time.sleep(2)
print(driver.title)

def login():

    anlage = driver.find_element(By.XPATH,'//*[@id="input-system"]')
    anlage.click()
    anlage.send_keys('xxxxx')

    benutzer = driver.find_element(By.XPATH,'//*[@id="input-username"]')
    benutzer.click()
    benutzer.send_keys('xxxx')

    kennwort = driver.find_element(By.XPATH,'//*[@id="input-password"]')
    kennwort.click()
    kennwort.send_keys('xxxx')

    login = driver.find_element(By.XPATH,'/html/body/app-root/app-login/div/div[2]/div/div/div/div/form/div[5]/button')
    login.click()

    time.sleep(3)
    print(driver.title)
    assert driver.title == 'Web Client'

I'm not sure if I need to instantiate the chrome driver again and if yes, how. I tried the command webdriver.Chrome() but it returns the error 'selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home'

Chromedriver.exe is alreday in the path however. Any help here is greatly appreciated.

PS: Apologies for any inconvenience with the website info, I'm not supposed to divulge this info as it is still under development internally. Thanks a lot!

Akash
  • 33
  • 3
  • Seems like your path is using "/" instead of "\". It will then fallback to using environment variable for Chromedriver PATH (which isn't set...) Just make sure you get the path right when passing it in to the constructor. I would also initialize the webdriver outside of your login function. (Seems like it should be above the wait= line.) Then pass it in to any functions/methods you are using. – pcalkins Aug 08 '22 at 16:52

0 Answers0