Skip to content

Instantly share code, notes, and snippets.

@gargomoma
Last active February 24, 2023 16:18
Show Gist options
  • Save gargomoma/0baf731e7ba43d63b6fee3aa8b3745f6 to your computer and use it in GitHub Desktop.
Save gargomoma/0baf731e7ba43d63b6fee3aa8b3745f6 to your computer and use it in GitHub Desktop.
My essential imports and functions to play with Selenium on Python
###My essential imports and functions to play with Selenium on Python
import os
import sys
import threading
import time
import datetime as dt
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import SessionNotCreatedException
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
ptime = lambda *x, end='\n':print(f"{dt.datetime.now().strftime('%m-%d %H:%M:%S')} -",*x,end=end)
def wait4elementByxPath(webDrvr,xPath):
present = False
counter = 0
obj = None
while present!= True:
if counter >= 50:
break
try:
time.sleep(1)
obj = webDrvr.find_element(by=By.XPATH, value=xPath)
present = True
except:
time.sleep(1)
counter = counter + 1
continue
return obj
############################################################
def wait4elementByCss(webDrvr,css_selector):
present = False
counter = 0
obj = None
while present!= True:
if counter >= 50:
break
try:
time.sleep(1)
obj = webDrvr.find_element(by=By.CSS_SELECTOR, value=css_selector)
present = True
except:
time.sleep(1)
counter = counter + 1
continue
return obj
############################################################
def LogInto(webDrvr,Username,Password):
amIlogged = False
authadress = 'https://'
try:
webDrvr.get(authadress)
time.sleep(2)
driver.find_element(by=By.XPATH, value='/html/body/nl-root/nl-login/div/div/div/div/div/nl-slide-panel/div/div[1]/div/nl-login-form/form/div[1]/nl-input-add-on/div/input').send_keys(Username)
driver.find_element(by=By.XPATH, value='/html/body/nl-root/nl-login/div/div/div/div/div/nl-slide-panel/div/div[1]/div/nl-login-form/form/div[2]/nl-input-add-on/div/input').send_keys(Password)
time.sleep(2)
driver.find_element(by=By.XPATH, value='/html/body/nl-root/nl-login/div/div/div/div/div/nl-slide-panel/div/div[1]/div/nl-login-form/form/button').click()
wait4elementByxPath(webDrvr,'/html/body/nl-root/ng-component/main/div/mat-drawer-container/mat-drawer-content/nl-system-dashboard/div/section/nl-summary-box/div/div/div[1]/div[1]')
amIlogged = True
except:
amIlogged = False
pass
return amIlogged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment