Best Python code snippet using lettuce_webdriver_python
css.py
Source:css.py  
...82    if len(elements) > 1:83        raise AssertionError("Multiple matching elements found.")84    return elements[0]85@load_jquery86def find_parents_by_jquery(browser, selector):87    """Find HTML elements' parents using jQuery-style selectors.88    Ensures that jQuery is available to the browser."""89    return browser.execute_script(90        """return ($ || jQuery)(arguments[0]).parent().get();""", selector)91@step(r'There should be an element matching \$\("(.*?)"\)$')92@wait_for93def check_element_by_selector(self, selector):94    """Assert an element exists matching the given selector."""95    elems = find_elements_by_jquery(world.browser, selector)96    if not elems:97        raise AssertionError("Expected matching elements, none found.")98@step(r'There should not be an element matching \$\("(.*?)"\)$')99@wait_for100def check_no_element_by_selector(self, selector):101    """Assert an element does not exist matching the given selector."""102    elems = find_elements_by_jquery(world.browser, selector)103    if elems:104        raise AssertionError("Expected no matching elements, found {}.".format(105            len(elems)))106@step(r'There should be an element matching \$\("(.*?)"\) '107      r'within (\d+) seconds?$')108def wait_for_element_by_selector(self, selector, seconds):109    """110    Assert an element exists matching the given selector within the given time111    period.112    """113    def assert_element_present():114        """Assert an element matching the given selector exists."""115        if not find_elements_by_jquery(world.browser, selector):116            raise AssertionError("Expected a matching element.")117    wait_for(assert_element_present)(timeout=int(seconds))118@step(r'There should be exactly (\d+) elements matching \$\("(.*?)"\)$')119@wait_for120def count_elements_exactly_by_selector(self, number, selector):121    """122    Assert n elements exist matching the given selector.123    """124    elems = find_elements_by_jquery(world.browser, selector)125    number = int(number)126    if len(elems) != number:127        raise AssertionError("Expected {} elements, found {}".format(128            number, len(elems)))129@step(r'I fill in \$\("(.*?)"\) with "(.*?)"$')130@wait_for131def fill_in_by_selector(self, selector, value):132    """Fill in the form element matching the CSS selector."""133    elem = find_element_by_jquery(world.browser, selector)134    elem.clear()135    elem.send_keys(value)136@step(r'I submit \$\("(.*?)"\)')137@wait_for138def submit_by_selector(self, selector):139    """Submit the form matching the CSS selector."""140    elem = find_element_by_jquery(world.browser, selector)141    elem.submit()142@step(r'I check \$\("(.*?)"\)$')143@wait_for144def check_by_selector(self, selector):145    """Check the checkbox matching the CSS selector."""146    elem = find_element_by_jquery(world.browser, selector)147    if not elem.is_selected():148        elem.click()149@step(r'I click \$\("(.*?)"\)$')150@wait_for151def click_by_selector(self, selector):152    """Click the element matching the CSS selector."""153    # No need for separate button press step with selector style.154    elem = find_element_by_jquery(world.browser, selector)155    elem.click()156@step(r'I follow the link \$\("(.*?)"\)$')157@wait_for158def follow_link_by_selector(self, selector):159    """160    Navigate to the href of the element matching the CSS selector.161    N.B. this does not click the link, but changes the browser's URL.162    """163    elem = find_element_by_jquery(world.browser, selector)164    href = elem.get_attribute('href')165    world.browser.get(href)166@step(r'\$\("(.*?)"\) should be selected$')167@wait_for168def is_selected_by_selector(self, selector):169    """Assert the option matching the CSS selector is selected."""170    elem = find_element_by_jquery(world.browser, selector)171    if not elem.is_selected():172        raise AssertionError("Element expected to be selected.")173@step(r'I select \$\("(.*?)"\)$')174@wait_for175def select_by_selector(self, selector):176    """Select the option matching the CSS selector."""177    option = find_element_by_jquery(world.browser, selector)178    selectors = find_parents_by_jquery(world.browser, selector)179    if not selectors:180        raise AssertionError("No parent element found for the option.")181    selector = selectors[0]182    selector.click()183    sleep(0.3)184    option.click()185    if not option.is_selected():186        raise AssertionError(...css_selector_steps.py
Source:css_selector_steps.py  
...38    """Find a single HTML element using jQuery-style selectors."""39    elements = find_elements_by_jquery(browser, selector)40    assert_true(step, len(elements) > 0)41    return elements[0]42def find_parents_by_jquery(browser, selector):43    """Find HTML elements' parents using jQuery-style selectors.44    In addition to reliably including jQuery, this also finds the pa"""45    try:46        return browser.execute_script("""return ($ || jQuery)(arguments[0]).parent().get();""", selector)47    except WebDriverException as e:48        if e.msg.startswith(u'$ is not defined'):49            load_script(browser, "//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js")50            return browser.execute_script("""return ($ || jQuery)(arguments[0]).parent().get();""", selector)51        else:52            raise53@step(r'There should be an element matching \$\("(.*?)"\)$')54def check_element_by_selector(step, selector):55    elems = find_elements_by_jquery(world.browser, selector)56    assert_true(step, elems)57@step(r'There should be an element matching \$\("(.*?)"\) within (\d+) seconds?$')58def wait_for_element_by_selector(step, selector, seconds):59    elems = wait_for_elem(world.browser, selector, timeout=int(seconds))60    assert_true(step, elems)61@step(r'There should be exactly (\d+) elements matching \$\("(.*?)"\)$')62def count_elements_exactly_by_selector(step, number, selector):63    elems = find_elements_by_jquery(world.browser, selector)64    assert_true(step, len(elems) == int(number))65@step(r'I fill in \$\("(.*?)"\) with "(.*?)"$')66def fill_in_by_selector(step, selector, value):67    elem = find_element_by_jquery(step, world.browser, selector)68    elem.clear()69    elem.send_keys(value)70@step(r'I submit \$\("(.*?)"\)')71def submit_by_selector(step, selector):72    elem = find_element_by_jquery(step, world.browser, selector)73    elem.submit()74@step(r'I check \$\("(.*?)"\)$')75def check_by_selector(step, selector):76    elem = find_element_by_jquery(step, world.browser, selector)77    if not elem.is_selected():78        elem.click()79@step(r'I click \$\("(.*?)"\)$')80def click_by_selector(step, selector):81    # No need for separate button press step with selector style.82    elem = find_element_by_jquery(step, world.browser, selector)83    elem.click()84@step(r'I follow the link \$\("(.*?)"\)$')85def click_by_selector(step, selector):86    elem = find_element_by_jquery(step, world.browser, selector)87    href = elem.get_attribute('href')88    world.browser.get(href)89@step(r'\$\("(.*?)"\) should be selected$')90def click_by_selector(step, selector):91    # No need for separate button press step with selector style.92    elem = find_element_by_jquery(step, world.browser, selector)93    assert_true(step, elem.is_selected())94@step(r'I select \$\("(.*?)"\)$')95def select_by_selector(step, selector):96    option = find_element_by_jquery(step, world.browser, selector)97    selectors = find_parents_by_jquery(world.browser, selector)98    assert_true(step, len(selectors) > 0)99    selector = selectors[0]100    selector.click()101    sleep(0.3)102    option.click()103    assert_true(step, option.is_selected())104@step(r'There should not be an element matching \$\("(.*?)"\)$')105def check_element_by_selector(step, selector):106    elems = find_elements_by_jquery(world.browser, selector)107    assert_false(step, elems)108__all__ = [109    'wait_for_element_by_selector',110    'fill_in_by_selector',111    'check_by_selector',...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
