def _by(selector: str): # --- Handle XPath --- if (selector.startswith('/') or selector.startswith('./') or selector.startswith('..') or selector.startswith('(')): return By.XPATH, selector # --- Handle custom conventions --- from selene import by if (selector.lower().startswith('text="') or selector.startswith("text='")): return by.text(selector[6:-1]) if (selector.startswith('«') and selector.endswith('»')): return by.text(selector[1:-1]) if selector.startswith('text='): return by.partial_text(selector[5:-0]) # --- Handle guessed selectors based on words --- lowered = selector.lower() if (is_word_with_dashes_underscores_or_numbers(selector) and not is_word_an_html_tag(selector)): return ( By.CSS_SELECTOR, '' + f'#{selector}' + (f',#{lowered}' if not selector.islower() else '') + f',[name={selector}]' + (f',[name={selector.lower()}]' if not selector.islower() else '') + f',.{selector}' + (f',.{lowered}' if not selector.islower() else '') + (f',[placeholder^={selector.capitalize()}]' if '_' not in selector else '')) # --- Handle CSS --- return By.CSS_SELECTOR, selector
def test_nested_elements_search(session_browser): page = GivenPage(session_browser.driver) page.opened_with_body(''' <div id="container"> <div> <div> <label>First</label> </div> <div> <a href="#first">go to Heading 1</a> </div> </div> <div> <div> <label>Second</label> <div> <a href="#second">go to Heading 2</a> <a href="#third">go to Heading 3</a> </div> </div> <div> </div> </div> </div> <h1 id="first">Heading 1</h2> <h2 id="second">Heading 2</h2> <h2 id="third">Heading 3</h2> ''') session_browser.element('#container').element( by.text('Second')).element('./following-sibling::*').element( by.partial_text('Heading 3')).click() assert "third" in session_browser.driver.current_url
def test_otus(): browser.open_url('https://otus.ru/') # print(browser.title()) # 'Онлайн курсы для профессионалов' browser.element(by.text('Отзывы')).click() el = browser.elements(by.css('.review-tile')).first().text print(el) browser.should(have.text('Selenium'))
def test_by_text(): assert by.text("test") == ( "xpath", './/*[text()[normalize-space(.) = concat("", "test")]]', )
def test_by_text_with_single_and_double_quotes(): given_active("""Fred's last name is "Li".""") assert find_element( by.text("""Fred's last name is "Li".""")).is_displayed()