forked from shubharthaksangharsha/apsara2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_selenium_functions.py
79 lines (73 loc) · 3.13 KB
/
helper_selenium_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#By Shubharthak
'''
This script used selenium to automate tasks.
it consist of some helper functions which helps in automating tasks.
'''
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import ElementClickInterceptedException
from selenium.common.exceptions import NoSuchElementException # Import the exception
from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.support.ui import Select
def operate(driver=None, xpath=None, click=False, type=False, instructions=None, verbose=True):
if driver is None:
print('Driver not provided')
return
if xpath is None:
print('Xpath not provided')
return
if instructions:
if verbose:
print('Inside Instructions')
for inst in instructions:
if 'click' in inst[0]:
if verbose:
print('Clicking the element')
WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.XPATH,inst[2]))).click()
if verbose:
print('Clicked the element')
if 'type' in inst[0]:
if verbose:
print('Clicking the element to type')
type_element = WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.XPATH,inst[2])))
type_element.click()
if verbose:
print('Clicked the element')
type_element.send_keys(inst[1])
if verbose:
print('typed the keys ')
if click:
if verbose:
print('Clicking the element')
print(xpath)
WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.XPATH,xpath))).click()
if verbose:
print('Clicked the element')
if type:
if verbose:
print('Clicking the element to type')
type_element = WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.XPATH,xpath)))
type_element.click()
if verbose:
print('Clicked the element')
type_element.send_keys(type)
if verbose:
print('typed the keys ')
def sign_in_linkedin(username, password):
sign_button_xpath = '//*[@id="main-content"]/div/form/p/button'
email_button_xpath = '//*[@id="session_key"]'
password_button_xpath = '//*[@id="session_password"]'
submit_button_xpath = '//*[@id="main-content"]/div/div[2]/form/div[2]/button'
operate(sign_button_xpath, click=True)
# operate(email_button_xpath, type=username)
# operate(password_button_xpath, type=password)
operate(submit_button_xpath, click=True)
if __name__ == '__main__':
pass