Skip to content

Instantly share code, notes, and snippets.

@diyan
Created March 31, 2014 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diyan/9888075 to your computer and use it in GitHub Desktop.
Save diyan/9888075 to your computer and use it in GitHub Desktop.
Selenium WebDriver monkey patch for using any CSS selector that jQuery supports.
"""
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