1
1
"""Module for logging into Eflyt/Daedalus/Whatchamacallit using Selenium"""
2
+ import time
3
+
2
4
from selenium import webdriver
3
5
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
5
20
6
21
7
- def login (username : str , password : str ) -> webdriver . Chrome :
22
+ def login (username : str , password : str ) -> ResilientBrowser :
8
23
"""Log into Eflyt using a password and username.
9
24
10
25
Args:
@@ -13,8 +28,9 @@ def login(username: str, password: str) -> webdriver.Chrome:
13
28
"""
14
29
chrome_options = webdriver .ChromeOptions ()
15
30
chrome_options .add_argument ("--disable-search-engine-choice-screen" )
16
- browser = webdriver . Chrome (options = chrome_options )
31
+ browser = ResilientBrowser (options = chrome_options )
17
32
browser .maximize_window ()
33
+ browser .implicitly_wait (2 )
18
34
19
35
browser .get ("https://notuskommunal.scandihealth.net/" )
20
36
browser .find_element (By .ID , "Login1_UserName" ).send_keys (username )
0 commit comments