Skip to content

Commit 24f00ad

Browse files
author
ravishankar
committed
added function clicks,extract page info,retrive cookies
1 parent c4ed82c commit 24f00ad

File tree

4 files changed

+143
-15
lines changed

4 files changed

+143
-15
lines changed

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,21 @@ stool is an wrapper over selenium to perform various task.
44

55

66
## Features
7-
- [X] Manage multiple webdrivers
7+
- [X] Manage multiple webdrivers.
8+
- [X] Click any type of element.
9+
- [X] Extract Page source.
10+
- [X] Select different type of elements.
11+
- [X] Retrive cookies.
12+
- [X] take fullpage and elementwise screenshots.
13+
- [X] display and hide elements.
14+
815

916
## TODO
10-
- [ ] Manage multiple webdrivers
17+
- [ ] Fill information(forms)
18+
- [ ] horizontal and vertical scrolling
19+
- [ ] Handeling alerts and popup
20+
- [ ] Switching windows,tabs,frames.
21+
- [ ] adding universal login functionality with forms
22+
- [ ] handling iframe windows.
23+
- [ ] Writing Parser to extract data from WebDriver.
24+
- [ ] Logging driver activities

stool/driver.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
from webdriver_manager.firefox import GeckoDriverManager
66
from webdriver_manager.utils import ChromeType
77

8-
from utils import (SToolException, current_url, get_element, get_session,
9-
page_source, visit)
8+
from utils import (SToolException, click, current_url, get_cookies,
9+
get_element, get_session, hide_show_elements, page_source,
10+
take_screenshot, visit)
1011

1112

1213
class SeleniumDriver:
14+
"""SeleniumDriver class to manage driver object and all utility functions at one place
15+
"""
1316

1417
def __init__(self, browser=None, headless=False):
1518
self.browser_list = ['chrome', 'chromium', 'firefox']
@@ -74,6 +77,10 @@ def _load_methods(self):
7477
self.text = partial(page_source, self.driver)
7578
self.url = partial(current_url, self.driver)
7679
self.element = partial(get_element, self.driver)
80+
self.click = partial(click, self.driver)
81+
self.cookies = partial(get_cookies, self.driver)
82+
self.screenshot = partial(take_screenshot, self.driver)
83+
self.hide = partial(hide_show_elements, self.driver)
7784

7885

7986
if __name__ == '__main__':
@@ -82,7 +89,3 @@ def _load_methods(self):
8289
with SeleniumDriver('firefox', headless=False) as obj:
8390
# visit https:example.com
8491
obj.visit(r'https://quotes.toscrape.com/')
85-
86-
# for all quotes obj.element('class_name', 'quote',1)
87-
result = obj.element('class_name', 'quote')
88-
print(result)

stool/utils/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# -*- coding: utf-8 -*-
22

33
from utils.driver_exceptions import SToolException
4-
from utils.driver_utils import (current_url, get_element, get_session,
5-
page_source, visit)
4+
from utils.driver_utils import (current_url, get_element, get_session, page_source,
5+
visit, get_locator, click, get_cookies, take_screenshot, hide_show_elements)
66

7-
__all__ = ["SToolException", "current_url", "get_element", "get_session",
8-
"page_source", "visit"]
7+
__all__ = ["SToolException", "current_url", "get_element", "get_session", "page_source",
8+
"visit", "get_locator", "click", "get_cookies", "take_screenshot", "hide_show_elements"]

stool/utils/driver_utils.py

Lines changed: 114 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
from selenium.common.exceptions import NoSuchElementException
1+
from selenium.common.exceptions import NoSuchElementException, TimeoutException
22
from selenium.webdriver.common.by import By
3+
from selenium.webdriver.support import expected_conditions as EC
4+
from selenium.webdriver.support.ui import WebDriverWait
35

46
from utils.driver_exceptions import SToolException
57

@@ -38,7 +40,8 @@ def get_locator(locator_type, locator_text):
3840
3941
locator_text : attribute value
4042
"""
41-
return getattr(By, locator_type), locator_text
43+
locator = locator_type.upper()
44+
return getattr(By, locator), locator_text
4245

4346

4447
def get_element(driver, locator_type, locator_text, many=None):
@@ -67,7 +70,115 @@ def get_element(driver, locator_type, locator_text, many=None):
6770
is_multiple = 's' if many else ''
6871
func = getattr(driver, f'find_element{is_multiple}')
6972
return func(*locator)
70-
except NoSuchElementException as _:
73+
except NoSuchElementException:
7174
return None
7275
else:
7376
raise SToolException("INVALID_SELECTOR")
77+
78+
79+
def click(driver, locator_type, locator_text, click_time=10):
80+
"""Return True if element clicked otherwise return None
81+
82+
Args:
83+
locator_type : provide any attribute type
84+
id,class_name,tag_name
85+
xpath, css_selector
86+
87+
locator_text : attribute value
88+
89+
Returns:
90+
True : If element clicked
91+
None : Not clicked or Not Found
92+
"""
93+
try:
94+
elem_locator = get_locator(locator_type, locator_text)
95+
element = WebDriverWait(driver, click_time).until(
96+
EC.element_to_be_clickable(elem_locator))
97+
element.click()
98+
return True
99+
except TimeoutException:
100+
return None
101+
except Exception as ex:
102+
raise SToolException(ex)
103+
104+
105+
def get_cookies(driver):
106+
"""Accept driver object and return cookies in dictionary
107+
108+
Args:
109+
driver : A selenium WebDriver
110+
111+
Returns:
112+
cookies_dict : return cookies in dicionary format if
113+
no cookies return an empty dictionary
114+
"""
115+
cookies = driver.get_cookies()
116+
cookies_dict = {cookie['name']: cookie['value'] for cookie in cookies}
117+
return cookies_dict or {}
118+
119+
120+
def take_screenshot(driver, element=None):
121+
"""take screenshot of given element if element is
122+
not given take a full page screeenshot and return
123+
data in bytes
124+
125+
Args:
126+
driver : selenium Webdriver
127+
element : default None, provide element locator
128+
example : element=('id','element_id')
129+
130+
Returns:
131+
returns byte object,if element not present
132+
it will return None.
133+
134+
full screenshot will work only in headless mode.
135+
"""
136+
if element and isinstance(element, tuple):
137+
locator_type, locator_text = element
138+
ele = get_element(driver, locator_type, locator_text)
139+
if ele:
140+
return ele.screenshot_as_png
141+
return None
142+
else:
143+
width = driver.execute_script("return document.body.offsetWidth")
144+
height = driver.execute_script("return document.body.offsetHeight")
145+
driver.set_window_size(width, height)
146+
return driver.get_screenshot_as_png()
147+
148+
149+
def display_element(driver, element, hide=None):
150+
""" hide or show single element
151+
152+
Args:
153+
driver : selenium webdriver
154+
element : an selenium element
155+
hide : default value is None, to hide element
156+
hide=1 to display hidden element
157+
Returns:
158+
None
159+
"""
160+
161+
hide_or_show = 'block' if hide else 'None'
162+
driver.execute_script(
163+
f"arguments[0].style.display = '{hide_or_show}';", element)
164+
165+
166+
def hide_show_elements(driver, elements, hide=None):
167+
"""hide or show multiple elements
168+
169+
Args:
170+
driver : selenium webdriver
171+
elements : list of tuples,[('locator_type','value')]
172+
example : [('id','id_value')]
173+
hide : default value is None, to hide element
174+
hide=1 to display hidden element
175+
176+
Returns:
177+
None
178+
"""
179+
for element_locator in elements:
180+
locator_type, locator_value = element_locator
181+
element_list = get_element(driver, locator_type, locator_value, 1)
182+
if element_list:
183+
for element in element_list:
184+
display_element(driver, element, hide)

0 commit comments

Comments
 (0)