Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed strict version dependency to selenium #109

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 37 additions & 7 deletions helium/_impl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from copy import copy
from distutils.version import StrictVersion
from helium._impl.match_type import PREFIX_IGNORE_CASE
from helium._impl.selenium_wrappers import WebElementWrapper, \
WebDriverWrapper, FrameIterator, FramesChangedWhileIterating
Expand All @@ -9,10 +10,12 @@
from inspect import getfullargspec, ismethod, isfunction
from os import access, X_OK
from os.path import join, dirname
import selenium
from selenium.common.exceptions import UnexpectedAlertPresentException, \
ElementNotVisibleException, MoveTargetOutOfBoundsException, \
WebDriverException, StaleElementReferenceException, \
NoAlertPresentException, NoSuchWindowException
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.ui import Select
Expand All @@ -22,6 +25,7 @@
import atexit
import re


def might_spawn_window(f):
def f_decorated(self, *args, **kwargs):
driver = self.require_driver()
Expand Down Expand Up @@ -787,19 +791,39 @@ def __init__(self, driver, selector, **kwargs):
def find_all_in_curr_frame(self):
wrap = lambda web_elements: list(map(WebElementWrapper, web_elements))
if self.selector.startswith('@'):
return wrap(self._driver.find_elements_by_name(self.selector[1:]))
if StrictVersion(selenium.__version__) >= StrictVersion('4.0.0'):
_xpath = wrap(self._driver.find_elements(By.NAME, self.selector[1:]))
else:
_xpath = wrap(self._driver.find_elements_by_name(self.selector[1:]))
return _xpath
if self.selector.startswith('//'):
return wrap(self._driver.find_elements_by_xpath(self.selector))
return wrap(self._driver.find_elements_by_css_selector(self.selector))
if StrictVersion(selenium.__version__) >= StrictVersion('4.0.0'):
_xpath = wrap(self._driver.find_elements(By.XPATH, self.selector))
else:
_xpath = wrap(self._driver.find_elements_by_xpath(self.selector))
return _xpath
if StrictVersion(selenium.__version__) >= StrictVersion('4.0.0'):
_xpath = wrap(self._driver.find_elements(By.CSS_SELECTOR, self.selector))
else:
_xpath = wrap(self._driver.find_elements_by_css_selector(self.selector))
return _xpath

class HTMLElementIdentifiedByXPath(HTMLElementImpl):
def find_all_in_curr_frame(self):
x_path = self.get_xpath()
return self._sort_search_result(
if StrictVersion(selenium.__version__) >= StrictVersion('4.0.0'):
_xpath = self._sort_search_result(
list(map(
WebElementWrapper, self._driver.find_elements_by_xpath(x_path)
WebElementWrapper, self._driver.find_elements(By.XPATH, x_path)
))
)
else:
_xpath = self._sort_search_result(
list(map(
WebElementWrapper, self._driver.find_elements_by_xpath(x_path)
))
)
return _xpath
def _sort_search_result(self, search_result):
keys_to_result_items = []
for web_elt in search_result:
Expand Down Expand Up @@ -939,9 +963,15 @@ def find_all_in_curr_frame(self):
def _find_elts(self, xpath=None):
if xpath is None:
xpath = self.get_xpath()
return list(map(
if StrictVersion(selenium.__version__) >= StrictVersion('4.0.0'):
_xpath = list(map(
WebElementWrapper, self._driver.find_elements(By.XPATH, xpath)
))
else:
_xpath = list(map(
WebElementWrapper, self._driver.find_elements_by_xpath(xpath)
))
))
return _xpath
def _find_elts_by_free_text(self):
elt_types = [
xpath.strip().lstrip('/') for xpath in self.get_xpath().split('|')
Expand Down
29 changes: 19 additions & 10 deletions helium/_impl/selenium_wrappers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from distutils.version import StrictVersion
from helium._impl.util.geom import Rectangle
from selenium.common.exceptions import StaleElementReferenceException, \
NoSuchFrameException, WebDriverException
import selenium
from selenium.webdriver.common.action_chains import ActionChains
from urllib.error import URLError
import sys


class Wrapper:
def __init__(self, target):
self.target = target
Expand Down Expand Up @@ -38,15 +41,21 @@ def get_distance_to_last_manipulated(self, web_element):
# No .location. This happens when last_manipulated_element is an
# Alert or a Window.
return 0
def find_elements_by_name(self, name):
# Selenium sometimes returns None. For robustness, we turn this into []:
return self.target.find_elements_by_name(name) or []
def find_elements_by_xpath(self, xpath):
# Selenium sometimes returns None. For robustness, we turn this into []:
return self.target.find_elements_by_xpath(xpath) or []
def find_elements_by_css_selector(self, selector):
# Selenium sometimes returns None. For robustness, we turn this into []:
return self.target.find_elements_by_css_selector(selector) or []
# Compare the version with '4.0.0'
if StrictVersion(selenium.__version__) >= StrictVersion('4.0.0'):
def find_elements(self, by, xpath):
# Selenium sometimes returns None. For robustness, we turn this into []:
return self.target.find_elements(by, xpath) or []
else:
def find_elements_by_name(self, name):
# Selenium sometimes returns None. For robustness, we turn this into []:
return self.target.find_elements_by_name(name) or []
def find_elements_by_xpath(self, xpath):
# Selenium sometimes returns None. For robustness, we turn this into []:
return self.target.find_elements_by_xpath(xpath) or []
def find_elements_by_css_selector(self, selector):
# Selenium sometimes returns None. For robustness, we turn this into []:
return self.target.find_elements_by_css_selector(selector) or []
def is_firefox(self):
return self.browser_name == 'firefox'
@property
Expand Down Expand Up @@ -166,4 +175,4 @@ def switch_to_frame(self, frame_index_path):
self.driver.switch_to.frame(frame_index)

class FramesChangedWhileIterating(Exception):
pass
pass
Binary file modified helium/_impl/webdrivers/linux/chromedriver
Binary file not shown.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
packages = find_packages(exclude=['tests', 'tests.*']),
install_requires = [
# Also update requirements/base.txt when you make changes here.
'selenium==3.141.0'
'selenium>=3.141.0'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This permits Selenium 3.141.0. Will your By. changes still work with Selenium 3?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose that to support both major release, a version check must be added.

],
package_data = {
'helium._impl': ['webdrivers/**/*']
Expand All @@ -37,4 +37,4 @@
'Operating System :: MacOS :: MacOS X'
],
test_suite='tests'
)
)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't unnecessarily change the white space. See https://gist.github.com/mherrmann/5ce21814789152c17abd91c0b3eaadca.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I immagine this is due to my editor settings.