-
Notifications
You must be signed in to change notification settings - Fork 0
/
selenium_with_nextcloud.py
152 lines (131 loc) · 5.95 KB
/
selenium_with_nextcloud.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from selenium import webdriver
from selenium.webdriver.common.by import By
import os
from time import sleep
import config
# TODO: add funcs description
# TODO: rewrite driver
def open_url_in_chrome(url, password=''):
'''main func'''
driver = webdriver.Chrome(config.driver_source)
driver.implicitly_wait(15)
driver.get(url)
if password != '':
driver.find_element(By.ID, 'password').send_keys(password)
driver.find_element(By.ID, 'password-submit').click()
return driver
def open_url_in_chrom_for_loading(url, path_for_download, password=''):
chrome_options = webdriver.ChromeOptions()
prefs = {'download.default_directory': path_for_download}
chrome_options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(config.driver_source, chrome_options=chrome_options)
driver.implicitly_wait(15)
driver.get(url)
if password != '':
driver.find_element(By.ID, 'password').send_keys(password)
driver.find_element(By.ID, 'password-submit').click()
return driver
def download(url, path_for_download, password=''):
driver = open_url_in_chrom_for_loading(url, path_for_download, password)
driver.find_element(By.XPATH, '//*[@id="headerSelection"]/label').click()
driver.find_element(By.CLASS_NAME, 'actions-selected').click()
driver.implicitly_wait(15)
driver.find_element(By.XPATH, '//*[@id="selectedActionsList"]/div/ul/li[2]/a').click()
sleep(10)
while any([filename.endswith(".crdownload") for filename in os.listdir(path_for_download)]):
sleep(1)
driver.quit()
def clear_cloud(url, password=''):
driver = open_url_in_chrome(url, password)
driver.find_element(By.XPATH, '//*[@id="headerSelection"]/label').click()
driver.find_element(By.CLASS_NAME, 'actions-selected').click()
driver.find_element(By.XPATH, '//*[@id="selectedActionsList"]/div/ul/li[3]/a').click()
sleep(5)
up_bar = ''
while up_bar != 'none':
up_bar = driver.find_element(By.ID, 'uploadprogressbar').value_of_css_property('display')
sleep(1)
sleep(2)
driver.quit()
def upload(url, files, password=''):
driver = open_url_in_chrome(url, password)
driver.implicitly_wait(15)
for file in files:
driver.find_element(By.XPATH, '//*[@id="file_upload_start"]').send_keys(file)
sleep(5)
up_bar = ''
while up_bar != 'none':
up_bar = driver.find_element(By.ID, 'uploadprogressbar').value_of_css_property('display')
sleep(1)
sleep(2)
driver.quit()
def create_folder(url, folders_name_list, password=''):
driver = open_url_in_chrome(url, password)
for n in folders_name_list:
try:
driver.find_element(By.CLASS_NAME, 'new').click()
driver.find_element(By.XPATH, '//*[@id="controls"]/div[2]/div[2]/ul/li[2]/a').click()
driver.find_element(By.XPATH, '//*[@id="view5-input-folder"]').send_keys(n)
driver.find_element(By.XPATH, '//*[@id="controls"]/div[2]/div[2]/ul/li[2]/a/form/input[2]').click()
sleep(3)
print(f'{n} folder created')
except Exception as e:
print('settings environments dirs error:', e, sep='\n')
driver.quit()
def create_folder_and_sub_folder(url, folder_name, subfolder_name, password=''):
"""description"""
driver = open_url_in_chrome(url, password)
try:
driver.find_element(By.CLASS_NAME, 'new').click()
driver.find_element(By.XPATH, '//*[@id="controls"]/div[2]/div[2]/ul/li[2]/a').click()
driver.find_element(By.XPATH, '//*[@id="view5-input-folder"]').send_keys(folder_name)
driver.find_element(By.XPATH, '//*[@id="controls"]/div[2]/div[2]/ul/li[2]/a/form/input[2]').click()
sleep(2)
# check this
driver.get(url+'%2F'+folder_name)
driver.find_element(By.CLASS_NAME, 'new').click()
driver.find_element(By.XPATH, '//*[@id="controls"]/div[2]/div[2]/ul/li[2]/a').click()
driver.find_element(By.XPATH, '//*[@id="view5-input-folder"]').send_keys(subfolder_name)
driver.find_element(By.XPATH, '//*[@id="controls"]/div[2]/div[2]/ul/li[2]/a/form/input[2]').click()
sleep(3)
except Exception as e:
print('settings environments dirs error:', e, sep='\n')
driver.quit()
def download_clear_create_folder(url, path_for_download, folders_name_list, password=''):
"""description"""
# download seattings
driver = open_url_in_chrom_for_loading(url, path_for_download, password)
# download
driver.find_element(By.XPATH, '//*[@id="headerSelection"]/label').click()
driver.find_element(By.CLASS_NAME, 'actions-selected').click()
driver.implicitly_wait(15)
driver.find_element(By.XPATH, '//*[@id="selectedActionsList"]/div/ul/li[2]/a').click()
sleep(10)
while any([filename.endswith(".crdownload") for filename in os.listdir(path_for_download)]):
sleep(3)
print('download competed')
# clear
driver.refresh()
driver.find_element(By.XPATH, '//*[@id="headerSelection"]/label').click()
driver.find_element(By.CLASS_NAME, 'actions-selected').click()
driver.find_element(By.XPATH, '//*[@id="selectedActionsList"]/div/ul/li[3]/a').click()
sleep(5)
up_bar = ''
while up_bar != 'none':
up_bar = driver.find_element(By.ID, 'uploadprogressbar').value_of_css_property('display')
sleep(1)
sleep(2)
print('clear competed')
# create
for n in folders_name_list:
try:
driver.find_element(By.CLASS_NAME, 'new').click()
driver.find_element(By.XPATH, '//*[@id="controls"]/div[2]/div[2]/ul/li[2]/a').click()
driver.find_element(By.XPATH, '//*[@id="view5-input-folder"]').send_keys(n)
driver.find_element(By.XPATH, '//*[@id="controls"]/div[2]/div[2]/ul/li[2]/a/form/input[2]').click()
sleep(3)
print('successfully created:',n, sep='\n')
except Exception as e:
print('error while creating:', n, sep='\n')
print('create competed')
driver.quit()