-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowserwindow.py
More file actions
148 lines (125 loc) · 4.03 KB
/
browserwindow.py
File metadata and controls
148 lines (125 loc) · 4.03 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
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
import os
from selenium import webdriver
from config import read_help_window_conf, save_help_window_conf
test_window = None
def test_call():
global test_window
""" if no window - create
if exists - save position/size and close """
if test_window is None:
c = read_help_window_conf()
#print(c)
o=webdriver.ChromeOptions()
o.add_argument("disable-infobars");
o.add_argument("--window-size=%(w)d,%(h)d"%c);
o.add_argument("--window-position=%(x)d,%(y)d"%c);
test_window=webdriver.Chrome(chrome_options=o, service_args=["--verbose"])
# test_window.set_window_position(c["x"], c["y"])
# test_window.set_window_size(c["w"], c["h"])
else:
#print(dir(test_window))
#print(test_window.get_window_position())
#print(test_window.get_window_size())
pos = test_window.get_window_position()
size = test_window.get_window_size()
test_window.close()
save_help_window_conf({'x': pos['x'], 'y': pos['y'], 'w': size['width'], 'h': size['height']})
test_window = None
help_window = None
help_window_conf = None
def create_window(c):
o=webdriver.ChromeOptions()
o.add_argument("disable-infobars");
o.add_argument("--window-size=%(w)d,%(h)d"%c);
o.add_argument("--window-position=%(x)d,%(y)d"%c);
#o.add_argument("--enable-logging");
#o.set_headless(True)
#o.add_argument("--homepage "+url);
return webdriver.Chrome(chrome_options=o)
#, service_args=["--verbose", "--log-path="+os.path.join(os.environ["TEMP"], "chromedriver.log")])
#, service_log_path = os.path.join(os.environ["TEMP"], "selenium.log"))
def show_help(url, retry=True):
global help_window, help_window_conf
#print("open url", url)
if help_window:
c = help_window_conf = read_help_window_conf()
try:
#print("try in existing window")
help_window.get(url) # do it first as only .get raises exception when chrome is lost; others just time-out
update_help_window_conf()
# help_window.set_window_position(c["x"], c["y"])
# help_window.set_window_size(c["w"], c["h"])
#print("done")
except Exception as e:
#print("error", str(e), "drop old window")
help_window = None
show_help(url, True)
else:
#print("need new window")
pass
# if help_window:
# print("already open", help_window.title)
# return
if retry:
#print("create new window")
c = help_window_conf = read_help_window_conf()
help_window = create_window(c)
show_help(url, False)
else:
#print("cannot show help")
pass
def update_help_window_conf():
global help_window, help_window_conf
if help_window:
pos = help_window.get_window_position()
size = help_window.get_window_size()
#print(pos, size)
update = False
if pos['x']!=help_window_conf['x']:
help_window_conf['x'] = pos['x']
update = True
if pos['y']!=help_window_conf['y']:
help_window_conf['y'] = pos['y']
update = True
if size['width']!=help_window_conf['w']:
help_window_conf['w'] = size['width']
update = True
if size['height']!=help_window_conf['h']:
help_window_conf['h'] = size['height']
update = True
if update:
#print("Save help window disposition", help_window_conf)
save_help_window_conf(help_window_conf)
#show_help("http://gmail.com/")
#print(dir(help_window))
# return help_window
# except:
# print("error in show_help")
def close_help():
global help_window
try:
if help_window:
update_help_window_conf()
help_window.close()
help_window=None
except:
#print("error in close_help")
pass
def test():
import time
try:
show_help("https://www.mail.ru")
time.sleep(2)
# show_help("https://www.yandex.ru")
# time.sleep(3)
# show_help("https://www.google.com")
# time.sleep(3)
finally:
pass
close_help()
if __name__=="__main__":
# import win32console, win32gui
# mywindow = win32console.GetConsoleWindow()
# win32gui.ShowWindow(mywindow, 0)
test()
# win32gui.ShowWindow(mywindow, 1)