-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPdfCompressor.py
92 lines (69 loc) · 2.51 KB
/
PdfCompressor.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
'''
compressPDF(browser, filepath):
this function upload the file to the browser object.( the browseris theone that compreses it !)
downloadCompPDF(browser):
After all the files are compressed it downloads the zip file.
It first waits for the button to get enable before cicking
Return 0 when there is no file to download.
'''
from selenium import webdriver
import os
import time
import TorFirefox
def main():
browser = TorFirefox.getFirefoxBrowser()
url = 'http://pdfcompressor.com/'
browser.get(url)
# dir_path = os.path.dirname(os.path.realpath(__file__))
##################################
# replace 2.pdf with the fileame #
##################################
file_path = os.getcwd() + "/2.pdf"
print file_path
compressPDF(browser, file_path)
flag1 = downloadCompPDF(browser)
if(flag1): #flag1 =0 when there is no fle to be downloaded
TorFirefox.unzipFile(os.getcwd()+"/pdfcompressor.zip",os.getcwd())
os.remove(os.getcwd()+"/pdfcompressor.zip")
browser.close()
def compressPDF(browser, filepath):
# browser = sel_tor1.getFirefoxBrowser()
# url = 'http://pdfcompressor.com/'
# browser.get(url)
# btn = browser.find_element_by_id('download-all')
# print btn.get_attribute('disabled')
print "======== compressPDF =========="
element = browser.find_element_by_xpath("//input[@type='file']")
element.send_keys(filepath)
ul = browser.find_element_by_id('filelist')
'''
# https://stackoverflow.com/questions/24795198/get-all-child-elements
all_children_by_xpath = ul.find_elements_by_xpath(".//*")
print 'len(all_children_by_xpath): ' + str(len(all_children_by_xpath))
'''
def downloadCompPDF(browser):
# https://stackoverflow.com/questions/24795198/get-all-child-elements
##check if there is file to be downloaded
ul = browser.find_element_by_id('filelist')
all_children_by_xpath = ul.find_elements_by_xpath(".//*")
print 'len(all_children_by_xpath): ' + str(len(all_children_by_xpath))
if (len(all_children_by_xpath) == 0):
print "NO file to dowload... Exit"
return 0
#download compressed pdf in zip file
print "======== DownloadCompPDF =========="
btn = browser.find_element_by_id('download-all')
i=1
print btn.get_attribute('disabled')
while( btn.get_attribute('disabled') ): # this condition returns true when the btn is disabled
time.sleep(5)
print "trial no = ", i
i = i + 1
print btn.get_attribute('disabled')
print "Buttton is now clickable"
browser.find_element_by_id('download-all').click()
# print elem
print browser
return 1
if __name__ == "__main__":
main()