Created
March 31, 2014 08:45
-
-
Save diyan/9888075 to your computer and use it in GitHub Desktop.
Selenium WebDriver monkey patch for using any CSS selector that jQuery supports.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Selenium WebDriver monkey patch for using any CSS selector that jQuery supports. | |
Useful urls: | |
Inject the Sizzle CSS selector library. Selenium WebDriver - http://selenium.polteq.com/en/injecting-the-sizzle-css-selector-library/ | |
Adding the Sizzle CSS Selector library in WebDriver - http://seleniumtestingworld.blogspot.com/2013/01/adding-sizzle-css-selector-library-and.html | |
""" | |
from __future__ import unicode_literals | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver | |
def find_element_by_css_selector(self, css_selector): | |
if ':contains' in css_selector: | |
if self.execute_script('return !!$'): | |
return self.execute_script('return $(arguments[0])[0]', css_selector) | |
elif self.execute_script('return !!Sizzle'): | |
return self.execute_script('return Sizzle(arguments[0])[0]', css_selector) | |
else: | |
raise Exception('JavaScript CSS selector engines not found') | |
return self.find_element(by=By.CSS_SELECTOR, value=css_selector) | |
RemoteWebDriver.find_element_by_css_selector = find_element_by_css_selector |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment