Home > front end >  Can't use old window in Selenium with firefox and python
Can't use old window in Selenium with firefox and python

Time:09-23

# filename: LudusLogin
import os
from dotenv import load_dotenv
from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.expected_conditions import title_is
from selenium.webdriver.support.wait import WebDriverWait
import time


def login():
    # Firefox driver for running window
    driver = Firefox()

    # Going to website
    driver.get("website")

    WebDriverWait(driver, timeout=3).until(title_is("Meddelelser"))

    # Login with credentials from .env
    load_dotenv()

    usr = os.getenv("usr")
    passwd = os.getenv("passwd")

    usr_field = driver.find_element(By.CSS_SELECTOR, "#brugerIdField")
    passwd_field = driver.find_element(By.CSS_SELECTOR, "#kodeordField")

    usr_field.send_keys(usr)
    passwd_field.send_keys(passwd)
    passwd_field.send_keys(Keys.RETURN)

    # Closing messages
    time.sleep(2)
    close_btn_meddelelser = driver.find_element(By.CLASS_NAME, "v-window-closebox")
    close_btn_meddelelser.click()
    WebDriverWait(driver, timeout=3).until(title_is("Aktuelt"))

    # window_handle = driver.window_handles
    window_handle = driver.window_handles[0]

    return window_handle

Next File I want to switch to the window from code above and continue executing code in the same window

# filename: Main.py
import LudusLogin
from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
from selenium.webdriver.support.expected_conditions import title_is
from selenium.webdriver.support.wait import WebDriverWait

handle = LudusLogin.login()
print(handle)
driver = Firefox(executable_path='geckodriver.exe')
driver.switch_to.window(handle)

skema_btn = driver.find_element(By.CSS_SELECTOR, "span.v-menubar-menuitem:nth-child(4)")
skema_btn.click()
WebDriverWait(driver, timeout=3).until(title_is("Skemaer"))

I want to be able to have all scripts for the website in Main.py or other files and just the login in one file. When I try as shown i get the error: selenium.common.exceptions.NoSuchWindowException: Message: Unable to locate window:

CodePudding user response:

Create new instance of Firefox driver in Main.py first:

driver = Firefox()

Alter login method and in LudusLogin.py and make a change to accept input variable named driver. Then delete line

driver = Firefox()

inside LudusLogin.py and let login body to use input variable driver.

Now in Main.py you have driver variable to use after calling login method. the driver which you instantiate only once.

CodePudding user response:

Thanks for the answer, this is what I ended up with


import os
from dotenv import load_dotenv
# from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.expected_conditions import title_is
from selenium.webdriver.support.wait import WebDriverWait
import time


def login(driver):

    # Going to website
    driver.get("website")

    WebDriverWait(driver, timeout=3).until(title_is("Meddelelser"))

    # Login with credentials from .env
    load_dotenv()

    usr = os.getenv("usr")
    passwd = os.getenv("passwd")

    usr_field = driver.find_element(By.CSS_SELECTOR, "#brugerIdField")
    passwd_field = driver.find_element(By.CSS_SELECTOR, "#kodeordField")

    usr_field.send_keys(usr)
    passwd_field.send_keys(passwd)
    passwd_field.send_keys(Keys.RETURN)

    # Closing messages
    time.sleep(2)
    close_btn_meddelelser = driver.find_element(By.CLASS_NAME, "v-window-closebox")
    close_btn_meddelelser.click()
    WebDriverWait(driver, timeout=3).until(title_is("Aktuelt"))


import LudusLogin
from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
from selenium.webdriver.support.expected_conditions import title_is
from selenium.webdriver.support.wait import WebDriverWait

driver = Firefox()
LudusLogin.login(driver)

skema_btn = driver.find_element(By.CSS_SELECTOR, "span.v-menubar-menuitem:nth-child(4)")
skema_btn.click()
WebDriverWait(driver, timeout=3).until(title_is("Skemaer"))

  • Related