Skip to content

Commit 4df2bb6

Browse files
Selenium cheatsheet
1 parent ac11f2b commit 4df2bb6

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed
-17 KB
Loading
-90.5 KB
Loading

Selenium/Cheatsheet.txt

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
from selenium.webdriver.support import expected_conditions as EC
2+
from selenium.webdriver.common.action_chains import ActionChains
3+
from webdriver_manager.chrome import ChromeDriverManager
4+
from webdriver_manager.firefox import GeckoDriverManager
5+
from selenium.webdriver.support.ui import WebDriverWait
6+
from selenium.webdriver.common.keys import Keys
7+
from selenium.webdriver.common.by import By
8+
from selenium import webdriver
9+
from bs4 import BeautifulSoup
10+
11+
12+
chrome_options = webdriver.ChromeOptions()
13+
chrome_options.add_argument("--incognito")
14+
chrome_options.add_argument("--start-maximized")
15+
chrome_options.add_argument("--enable-automation")
16+
chrome_options.add_argument("--disable-extensions")
17+
chrome_options.add_argument("--disable-popup-blocking")
18+
self.driver = webdriver.Chrome(ChromeDriverManager().install(),options=chrome_options)
19+
20+
21+
22+
webdriver.ActionChains(self.driver).send_keys(self.keyword).send_keys(Keys.ENTER).perform()
23+
webdriver.ActionChains(self.driver).send_keys(Keys.ESCAPE).perform()
24+
webdriver.ActionChains(self.driver).send_keys(Keys.ENTER).perform()
25+
26+
html = self.driver.page_source
27+
soup = BeautifulSoup(html,'html5lib')
28+
29+
searchList = soup.select("div[class*="+ "'" + some_name + "'" +"]")
30+
searchList = soup.find_all('div',attrs={'class': 'some_name'})
31+
for s in searchList:
32+
a = s.find('div',attrs={'class': 'some_name'})
33+
if a :
34+
a = a.get_text()
35+
36+
37+
WebDriverWait(self.driver, 60).until(EC.presence_of_element_located((By.CLASS_NAME, self.elem['chatlist'])))
38+
javaScriptHeight = "return document.getElementById('pane-side').scrollHeight;"
39+
# Scroll of Div elem
40+
height = int(self.driver.execute_script(javaScriptHeight))
41+
for i in range(0,height,1000):
42+
javaScriptScroll = "document.getElementById('pane-side').scrollTo(0,"+i+");"
43+
self.driver.execute_script(javaScriptScroll)
44+
45+
46+
# for dynamic nature
47+
thing = 'cool'
48+
some = self.driver.find_element_by_xpath("//div[@class="+"'"+thing+"'"+"]").text
49+
self.driver.find_element_by_xpath("div[@class="+"'"+ok+"'"+"]").click()
50+
self.driver.find_element_by_xpath("//input[@accept='*']").send_keys(doc)
51+
infoBar = self.driver.find_element_by_xpath("//a[@interactionclass=""'" +info+"'" "]")
52+
53+
# for classes with same name in first half
54+
# e.g ZZZ a , ZZZ b ZZZ c
55+
variable = 'ZZZ '
56+
selected = soup.find_all('div',
57+
{
58+
'class': lambda x: x
59+
and variable in x.split()
60+
}
61+
)
62+
63+
64+
regex = re.compile('^.*copyable-text.*$')
65+
text = s.find('div',attrs={'class': regex})
66+
67+
68+
# For scrolling up an div elem window
69+
scrollUp = "document.getElementsByClassName("+"'"+scroll+"'"+")[0].scroll(0,-500);"
70+
last_height = self.driver.execute_script(javaScript)
71+
while True:
72+
try:
73+
self.driver.execute_script(up)
74+
time.sleep(SCROLL_PAUSE_TIME)
75+
new_height = self.driver.execute_script(javaScript)
76+
if new_height == last_height :
77+
break
78+
last_height = new_height
79+
except :
80+
break
81+
82+
83+
self.driver.execute_script("window.onbeforeunload = function() {};")
84+
self.driver.execute_script("window.open('"+url+"', '_self')")
85+
self.driver.implicitly_wait(2)
86+
87+
videosXpath = self.driver.find_elements_by_xpath("//*[@class='abc']")
88+
for v in videosXpath:
89+
video = v.get_attribute("src")
90+
fileName = 'abc.mp4'
91+
r = requests.get(url)
92+
if r.status_code == 200:
93+
with open(fileName, 'wb') as f:
94+
f.write(r.content)
95+
96+
height = str(self.driver.execute_script("return document.body.scrollHeight"))
97+
height = "-"+height
98+
js = "window.scrollTo(0, "+height+");"
99+
self.driver.execute_script(js)
100+
101+
102+
Photo = soup.find('img',attrs = {'class' : 'PhotoImg photo img'})
103+
if Photo:
104+
Photo = Photo.get('src')
105+
path = self.filePathPhotos + os.sep + self.name + '_Photo.jpg'
106+
urllib.urlretrieve(Photo,path )
107+
print('File saved at = {}'.format(path))
108+
109+
110+
# Helps to scroll down Fast
111+
def scollEnd(self):
112+
SCROLL_PAUSE_TIME = 4
113+
last_height = self.driver.execute_script("return document.body.scrollHeight")
114+
while True:
115+
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") # Scroll down to bottom
116+
time.sleep(SCROLL_PAUSE_TIME) # Wait to load page
117+
new_height = self.driver.execute_script("return document.body.scrollHeight")
118+
print('Old height = {} , New Height = {}'.format(last_height,new_height))
119+
if new_height == last_height or self.terminate :
120+
return
121+
last_height = new_height
122+
123+
124+
# Helps to scroll down smoothly
125+
def scrollEnd(self):
126+
SCROLL_PAUSE_TIME = 4
127+
# Get scroll height
128+
# last_height = self.driver.execute_script("return document.body.scrollHeight")
129+
last_height = self.driver.execute_script("return window.pageYOffset")
130+
height = 350
131+
while True:
132+
down = str(height)
133+
js = "window.scrollTo(0, "+down+");"
134+
self.driver.execute_script(js)
135+
height += 350
136+
time.sleep(SCROLL_PAUSE_TIME)
137+
new_height = self.driver.execute_script("return window.pageYOffset")
138+
if new_height == last_height :
139+
break
140+
last_height = new_height
141+

0 commit comments

Comments
 (0)