-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSelenium_cheat_sheet.py
More file actions
133 lines (91 loc) · 4.13 KB
/
Copy pathSelenium_cheat_sheet.py
File metadata and controls
133 lines (91 loc) · 4.13 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
# import selenium
from selenium import webdriver
# setup driver's
firefoxdriver = webdriver.Firefox(executable_path="Path to Firefox driver")
# link to driver "https://github.com/mozilla/geckodriver/releases"
chromedriver = webdriver.Chrome(executable_path="Path to Chrome driver")
# link to driver "https://sites.google.com/a/chromium.org/chromedriver/downloads"
iedriver = webdriver.IE(executable_path="Path to IEDriverServer.exe")
# link to driver "http://selenium-release.storage.googleapis.com/index.html"
edgedriver = webdriver.Edge(executable_path="Path to MicrosoftWebDriver.exe")
# link to driver "https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/"
# webdriver.Edge(executable_path = 'C:\\Users\\Downloads\\edgedriver_win32\\msedgedriver.exe')
operadriver = webdriver.Opera(executable_path="Path to operadriver")
# link to driver "https://github.com/operasoftware/operachromiumdriver/releases"
# SafariDriver now requires manual installation of the extension prior to automation !!!
# Options, browser arguments
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--headless") # open browser in headless mode. Works in both Chrome and Firefox browsers
options.add_argument("--incognito") # open private chrome browser
options.add_argument("--start-maximized") # start browser maximized to screen. Requires only for Chrome browser. Firefox by default starts maximized
options.add_argument("--disable-notifications") # disable notifications, works Only in Chrome browser
driver = webdriver.Chrome(chrome_options=options, executable_path="Path to driver")
# OR
options = Options()
options.add_argument("--incognito", "--start-maximized", "--headless")
driver = webdriver.Chrome(chrome_options=options, executable_path="Path to driver")
# go to some URL
driver.get("https://www...") # go to page
driver.back()
driver.forward()
driver.refresh() # update page
# get browser details
driver.title
driver.window_handles
driver.current_window_handles
driver.current_url
driver.page_source
# get some element or elements
driver.find_element_by_ # return first element matching the given locator argument
driver.find_elements_by_ # return a list with all elements matching the given locator argument
# by id
# <input id="q" type="text" />
element = driver.find_element_by_id("q")
# by name
# <input id="q" name="search" type="text" />
element = driver.find_element_by_name("search")
# by class_name
# <div class="username" style="display: block;">…</div>
element = driver.find_element_by_class_name("username")
# cklick on checkbox with javascript
driver.execute_script("document.getElementById('personalData').checked = true")
click = driver.find_element_by_class_name('checkbox checkbox-off')
click.click()
# scroll UP page
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.HOME)
# scroll DOWN page
browser.execute_script("window.scrollTo(0,document.body.scrollHeight)")
# driver quit
driver.quit()
# or driver.close()
# using Try, Finally
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
try:
element = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.ID, "id-of-new-element"))
)
finally:
driver.quit()
# disable images
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images": 2}
chrome_options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)
# get image src
driver.find_element_by_id("element_id").get_attribute("src")
# or
image = driver.find_element(By.XPATH, value='//*[@id="app"]//div[1]/img')
print(image.get_attribute("scr"))
wait = WebDriverWait(browser, 20)
actions = ActionChains(browser)
product_img_xpath = '//div[contains(@class,"s-item")]//img'
wait.until(EC.visibility_of_element_located((By.XPATH, product_img_xpath)))
time.sleep(1)
imgs = browser.find_elements_by_xpath(product_img_xpath)
for img in imgs:
actions.move_to_element(img).perform()
print(img.get_attribute('src'))