Skip to content

Commit 5fd2671

Browse files
committed
Eflyt change to login fixing stale element
1 parent 7a41a15 commit 5fd2671

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

itk_dev_shared_components/eflyt/eflyt_login.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
"""Module for logging into Eflyt/Daedalus/Whatchamacallit using Selenium"""
2+
import time
3+
24
from selenium import webdriver
35
from selenium.webdriver.common.by import By
4-
from selenium.common.exceptions import NoSuchElementException
6+
from selenium.webdriver.remote.webelement import WebElement
7+
from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException
8+
9+
10+
class ResilientBrowser(webdriver.Chrome):
11+
"""A webdriver.Chrome subclass that retries find_element if the element goes stale."""
12+
def find_element(self, by=By.ID, value = None) -> WebElement:
13+
element = super().find_element(by, value)
14+
time.sleep(0.1)
15+
try:
16+
element.tag_name
17+
except StaleElementReferenceException:
18+
element = super().find_element(by, value)
19+
return element
520

621

7-
def login(username: str, password: str) -> webdriver.Chrome:
22+
def login(username: str, password: str) -> ResilientBrowser:
823
"""Log into Eflyt using a password and username.
924
1025
Args:
@@ -13,8 +28,9 @@ def login(username: str, password: str) -> webdriver.Chrome:
1328
"""
1429
chrome_options = webdriver.ChromeOptions()
1530
chrome_options.add_argument("--disable-search-engine-choice-screen")
16-
browser = webdriver.Chrome(options=chrome_options)
31+
browser = ResilientBrowser(options=chrome_options)
1732
browser.maximize_window()
33+
browser.implicitly_wait(2)
1834

1935
browser.get("https://notuskommunal.scandihealth.net/")
2036
browser.find_element(By.ID, "Login1_UserName").send_keys(username)

0 commit comments

Comments
 (0)