forked from cherkavi/python-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselenium_utils.py
More file actions
83 lines (69 loc) · 2.59 KB
/
selenium_utils.py
File metadata and controls
83 lines (69 loc) · 2.59 KB
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
80
81
82
83
import os
from time import sleep
from typing import Optional
from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.remote.webelement import WebElement
def parse_bool_value(str_value: str) -> Optional[bool]:
if str_value.strip().lower() in ["true", "ok", "yes"]:
return True
if str_value.strip().lower() in ["false", "ko", "no"]:
return False
return None
def create_virtual_display(show_display: bool) -> Display:
"""
should be created before using #create_browser
in case of using virtual display
"""
virtual_display: Display = Display(visible=show_display, size=(1024, 768))
virtual_display.start()
os.environ["DISPLAY"] = virtual_display.new_display_var
return virtual_display
def create_browser(path_to_geckodriver: str) -> WebDriver:
"""
https://github.com/mozilla/geckodriver/releases
full path to Gecko driver like "/home/soft/selenium_driver/geckodriver"
"""
return webdriver.Firefox(executable_path=path_to_geckodriver)
def pass_xing_login(driver: WebDriver, xing_login: str, xing_pass: str) -> bool:
"""
pass login on XING using driver and login/password
:Returns:
* True - login successfull
* False - can't login
"""
url = 'https://loginix.com/'
driver.get(url)
sleep(3)
try:
permission_accept:WebElement = driver.find_element_by_xpath('//*[@id="consent-accept-button"]')
permission_accept.click()
except NoSuchElementException:
# no question about privacy
pass
try:
driver.find_element_by_name("username").send_keys(xing_login)
except NoSuchElementException:
print("no element by name: username")
return False
try:
driver.find_element_by_name("password").send_keys(xing_pass)
except NoSuchElementException:
print("no element by name: password")
return False
try:
driver.find_element_by_xpath(
"/html/body/div[1]/div[2]/div/div[2]/section/div/main/div/div/div/div/div/form/div[5]/button/div").click()
except NoSuchElementException:
print("no element button login ")
return False
sleep(2)
try:
driver.find_element_by_xpath("/html/body/div[1]/div[2]/div/div[2]/section/div/main/div/div/div/div/div[2]/div[2]/button[1]/div/span").click()
sleep(2)
except NoSuchElementException:
# no element 'Try two factor authentication' with button Skip
pass
return True