-
Notifications
You must be signed in to change notification settings - Fork 1
/
selenium_basic
259 lines (169 loc) · 6.51 KB
/
selenium_basic
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
[url=https://selenium-python.readthedocs.io/waits.html]selenium waits[/url]
[url=https://selenium-python.readthedocs.io/locating-elements.html]locating elements[/url]
[url=https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.by.html]By locators[/url]
###selenium wait
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
...
WebDriverWait(browser, 3).until(EC.presence_of_element_located((By.ID, 'global-new-tweet-button')))
###selenium push keys
#options: https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
...
ActionChains(browser).key_down(Keys.COMMAND).send_keys("s").key_up(Keys.COMMAND).perform()
###headless
options = Options()
options.add_argument("--headless")
browser = webdriver.Chrome(chromedriver, chrome_options=options)
###scroll to bottom of page
def scroll_to_bottom(driver):
driver = self.browser
SCROLL_PAUSE_TIME = 0.5
# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
# Scroll down to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Wait to load page
time.sleep(SCROLL_PAUSE_TIME)
# Calculate new scroll height and compare with last scroll height
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
####remove popup for asking to save password
def chrome_prep(self):
'''get rid of asking to save password and notifications popup'''
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option(
'prefs', {
'credentials_enable_service': False,
"profile.default_content_setting_values.notifications" : 2,
'profile': {
'password_manager_enabled': False
}
}
)
return chrome_options
def setup_chrome(self):
options = self.chrome_prep()
self.browser = webdriver.Chrome(CHROMEPATH, chrome_options=options)
###switch tabs
# Opens a new tab
self.driver.execute_script("window.open()")
# Switch to the newly opened tab
self.driver.switch_to.window(self.driver.window_handles[1])
# Navigate to new URL in new tab
self.driver.get("https://google.com")
# Run other commands in the new tab here
You're then able to close the original tab as follows
# Switch to original tab
self.driver.switch_to.window(self.driver.window_handles[0])
# Close original tab
self.driver.close()
# Switch back to newly opened tab, which is now in position 0
self.driver.switch_to.window(self.driver.window_handles[0])
Or close the newly opened tab
# Close current tab
self.driver.close()
# Switch back to original tab
self.driver.switch_to.window(self.driver.window_handles[0])
from selenium import webdriver
import time
import os
URL = 'https://www.facebook.com/'
CHROMEPATH = '/home/metulburr/chromedriver'
PHANTOMPATH = '/home/metulburr/phantomjs'
EMAIL = ''
PASSWORD = ''
class App:
def __init__(self):
self.setup_chrome()
#self.setup_headless()
self.login()
self.to_home()
self.to_friends()
time.sleep(100000) #keep alive to view html
def delay(self):
time.sleep(3)
def chrome_prep(self):
'''get rid of asking to save password and notifications popup'''
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option(
'prefs', {
'credentials_enable_service': False,
"profile.default_content_setting_values.notifications" : 2,
'profile': {
'password_manager_enabled': False
}
}
)
return chrome_options
def setup_chrome(self):
options = self.chrome_prep()
os.environ["webdriver.chrome.driver"] = CHROMEPATH
self.browser = webdriver.Chrome(CHROMEPATH, chrome_options=options)
self.browser.set_window_position(0,0)
self.delay()
def setup_headless(self):
self.browser = webdriver.PhantomJS(PHANTOMPATH)
self.delay()
def login(self):
self.browser.get(URL)
time.sleep(1)
username = self.browser.find_element_by_id("email")
password = self.browser.find_element_by_id("pass")
username.send_keys(EMAIL)
password.send_keys(PASSWORD)
login_attempt = self.browser.find_element_by_xpath("//*[@type='submit']")
login_attempt.submit()
self.delay()
def to_home(self):
self.browser.execute_script("document.getElementsByClassName('linkWrap noCount')[0].click()")
self.delay()
def to_friends(self):
self.browser.execute_script("document.getElementsByClassName('_6-6')[2].click()")
self.delay()
def scroll_to_bottom(self.):
SCROLL_PAUSE_TIME = 0.5
# Get scroll height
last_height = self.browser.execute_script("return document.body.scrollHeight")
while True:
# Scroll down to bottom
self.browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Wait to load page
time.sleep(SCROLL_PAUSE_TIME)
# Calculate new scroll height and compare with last scroll height
new_height = self.browser.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
App()
#wait until example
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
import time
EMAIL = ''
PASSWORD = ''
CHROMEPATH = ''
login_url = 'https://www.google.com/accounts/Login'
browser = webdriver.Chrome(CHROMEPATH)
browser.set_window_position(0,0)
wait = WebDriverWait(browser, 2)
presence = EC.presence_of_element_located
visible = EC.visibility_of_element_located
browser.get(login_url)
wait.until(presence((By.ID, 'identifierId')))
browser.find_element_by_id("identifierId").send_keys(EMAIL)
browser.find_element_by_id("identifierNext").click()
wait.until(presence((By.NAME, 'password')))
browser.find_element_by_name("password").send_keys(PASSWORD)
browser.find_element_by_id("passwordNext").click()
wait.until(visible((By.CSS_SELECTOR, '#gb > div.gb_zd.gb_5d > div.gb_yc.gb_lb.gb_xc.gb_Zd > div > div.gb_ib.gb_4c.gb_yg.gb_R.gb_pf.gb_pb > div > a > span')))
soup = BeautifulSoup(browser.page_source, 'html.parser')
print(soup.prettify())