Path: blob/trunk/py/test/selenium/webdriver/common/driver_element_finding_tests.py
1865 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.1617import pytest1819from selenium.common.exceptions import InvalidSelectorException, NoSuchElementException20from selenium.webdriver.common.by import By2122# By.id positive232425def test_should_be_able_to_find_asingle_element_by_id(driver, pages):26pages.load("xhtmlTest.html")27element = driver.find_element(By.ID, "linkId")28assert element.get_attribute("id") == "linkId"293031def test_should_be_able_to_find_asingle_element_by_numeric_id(driver, pages):32pages.load("nestedElements.html")33element = driver.find_element(By.ID, "2")34assert element.get_attribute("id") == "2"353637def test_should_be_able_to_find_an_element_with_css_escape(driver, pages):38pages.load("idElements.html")39element = driver.find_element(By.ID, "with.dots")40assert element.get_attribute("id") == "with.dots"414243def test_should_be_able_to_find_multiple_elements_by_id(driver, pages):44pages.load("nestedElements.html")45elements = driver.find_elements(By.ID, "test_id")46assert len(elements) == 2474849def test_should_be_able_to_find_multiple_elements_by_numeric_id(driver, pages):50pages.load("nestedElements.html")51elements = driver.find_elements(By.ID, "2")52assert len(elements) == 8535455# By.id negative565758def test_should_not_be_able_to_locate_by_id_asingle_element_that_does_not_exist(driver, pages):59pages.load("formPage.html")60with pytest.raises(NoSuchElementException):61driver.find_element(By.ID, "non_Existent_Button")626364def test_should_not_be_able_to_locate_by_id_multiple_elements_that_do_not_exist(driver, pages):65pages.load("formPage.html")66elements = driver.find_elements(By.ID, "non_Existent_Button")67assert len(elements) == 0686970def test_finding_asingle_element_by_empty_id_should_throw(driver, pages):71pages.load("formPage.html")72with pytest.raises(NoSuchElementException):73driver.find_element(By.ID, "")747576def test_finding_multiple_elements_by_empty_id_should_return_empty_list(driver, pages):77pages.load("formPage.html")78elements = driver.find_elements(By.ID, "")79assert len(elements) == 0808182def test_finding_asingle_element_by_id_with_space_should_throw(driver, pages):83pages.load("formPage.html")84with pytest.raises(NoSuchElementException):85driver.find_element(By.ID, "nonexistent button")868788def test_finding_multiple_elements_by_id_with_space_should_return_empty_list(driver, pages):89pages.load("formPage.html")90elements = driver.find_elements(By.ID, "nonexistent button")91assert len(elements) == 0929394def test_no_such_element_error(driver, pages):95pages.load("formPage.html")96msg = r"\/errors#nosuchelementexception"97with pytest.raises(NoSuchElementException, match=msg):98driver.find_element(By.ID, "non_Existent_Button")99100101# By.name positive102103104def test_should_be_able_to_find_asingle_element_by_name(driver, pages):105pages.load("formPage.html")106element = driver.find_element(By.NAME, "checky")107assert element.get_attribute("value") == "furrfu"108109110def test_should_be_able_to_find_multiple_elements_by_name(driver, pages):111pages.load("nestedElements.html")112elements = driver.find_elements(By.NAME, "checky")113assert len(elements) > 1114115116def test_should_be_able_to_find_an_element_that_does_not_support_the_name_property(driver, pages):117pages.load("nestedElements.html")118element = driver.find_element(By.NAME, "div1")119assert element.get_attribute("name") == "div1"120121122# By.name negative123124125def test_should_not_be_able_to_locate_by_name_asingle_element_that_does_not_exist(driver, pages):126pages.load("formPage.html")127with pytest.raises(NoSuchElementException):128driver.find_element(By.NAME, "non_Existent_Button")129130131def test_should_not_be_able_to_locate_by_name_multiple_elements_that_do_not_exist(driver, pages):132pages.load("formPage.html")133elements = driver.find_elements(By.NAME, "non_Existent_Button")134assert len(elements) == 0135136137def test_finding_asingle_element_by_empty_name_should_throw(driver, pages):138pages.load("formPage.html")139with pytest.raises(NoSuchElementException):140driver.find_element(By.NAME, "")141142143def test_finding_multiple_elements_by_empty_name_should_return_empty_list(driver, pages):144pages.load("formPage.html")145elements = driver.find_elements(By.NAME, "")146assert len(elements) == 0147148149def test_finding_asingle_element_by_name_with_space_should_throw(driver, pages):150pages.load("formPage.html")151with pytest.raises(NoSuchElementException):152driver.find_element(By.NAME, "nonexistent button")153154155def test_finding_multiple_elements_by_name_with_space_should_return_empty_list(driver, pages):156pages.load("formPage.html")157elements = driver.find_elements(By.NAME, "nonexistent button")158assert len(elements) == 0159160161# By.tag_Name positive162163164def test_should_be_able_to_find_asingle_element_by_tag_name(driver, pages):165pages.load("formPage.html")166element = driver.find_element(By.TAG_NAME, "input")167assert element.tag_name.lower() == "input"168169170def test_should_be_able_to_find_multiple_elements_by_tag_name(driver, pages):171pages.load("formPage.html")172elements = driver.find_elements(By.TAG_NAME, "input")173assert len(elements) > 1174175176# By.tag_Name negative177178179def test_should_not_be_able_to_locate_by_tag_name_asingle_element_that_does_not_exist(driver, pages):180pages.load("formPage.html")181with pytest.raises(NoSuchElementException):182driver.find_element(By.TAG_NAME, "non_Existent_Button")183184185def test_should_not_be_able_to_locate_by_tag_name_multiple_elements_that_do_not_exist(driver, pages):186pages.load("formPage.html")187elements = driver.find_elements(By.TAG_NAME, "non_Existent_Button")188assert len(elements) == 0189190191@pytest.mark.xfail_firefox(reason="https://github.com/mozilla/geckodriver/issues/2007")192@pytest.mark.xfail_remote(reason="https://github.com/mozilla/geckodriver/issues/2007")193@pytest.mark.xfail_safari(reason="unlike chrome, safari raises NoSuchElementException")194@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")195@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")196def test_finding_asingle_element_by_empty_tag_name_should_throw(driver, pages):197pages.load("formPage.html")198with pytest.raises(InvalidSelectorException):199driver.find_element(By.TAG_NAME, "")200201202@pytest.mark.xfail_firefox(reason="https://github.com/mozilla/geckodriver/issues/2007")203@pytest.mark.xfail_remote(reason="https://github.com/mozilla/geckodriver/issues/2007")204@pytest.mark.xfail_safari(reason="unlike chrome, safari returns an empty list")205@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")206@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")207def test_finding_multiple_elements_by_empty_tag_name_should_throw(driver, pages):208pages.load("formPage.html")209with pytest.raises(InvalidSelectorException):210driver.find_elements(By.TAG_NAME, "")211212213def test_finding_asingle_element_by_tag_name_with_space_should_throw(driver, pages):214pages.load("formPage.html")215with pytest.raises(NoSuchElementException):216driver.find_element(By.TAG_NAME, "nonexistent button")217218219def test_finding_multiple_elements_by_tag_name_with_space_should_return_empty_list(driver, pages):220pages.load("formPage.html")221elements = driver.find_elements(By.TAG_NAME, "nonexistent button")222assert len(elements) == 0223224225# By.class_Name positive226227228def test_should_be_able_to_find_asingle_element_by_class(driver, pages):229pages.load("xhtmlTest.html")230element = driver.find_element(By.CLASS_NAME, "extraDiv")231assert "Another div starts here." in element.text232233234def test_should_be_able_to_find_multiple_elements_by_class_name(driver, pages):235pages.load("xhtmlTest.html")236elements = driver.find_elements(By.CLASS_NAME, "nameC")237assert len(elements) > 1238239240def test_should_find_element_by_class_when_it_is_the_first_name_among_many(driver, pages):241pages.load("xhtmlTest.html")242element = driver.find_element(By.CLASS_NAME, "nameA")243assert element.text == "An H2 title"244245246def test_should_find_element_by_class_when_it_is_the_last_name_among_many(driver, pages):247pages.load("xhtmlTest.html")248element = driver.find_element(By.CLASS_NAME, "nameC")249assert element.text == "An H2 title"250251252def test_should_find_element_by_class_when_it_is_in_the_middle_among_many(driver, pages):253pages.load("xhtmlTest.html")254element = driver.find_element(By.CLASS_NAME, "nameBnoise")255assert element.text == "An H2 title"256257258def test_should_find_element_by_class_when_its_name_is_surrounded_by_whitespace(driver, pages):259pages.load("xhtmlTest.html")260element = driver.find_element(By.CLASS_NAME, "spaceAround")261assert element.text == "Spaced out"262263264def test_should_find_elements_by_class_when_its_name_is_surrounded_by_whitespace(driver, pages):265pages.load("xhtmlTest.html")266elements = driver.find_elements(By.CLASS_NAME, "spaceAround")267assert len(elements) == 1268assert elements[0].text == "Spaced out"269270271# By.class_Name negative272273274def test_should_not_find_element_by_class_when_the_name_queried_is_shorter_than_candidate_name(driver, pages):275pages.load("xhtmlTest.html")276with pytest.raises(NoSuchElementException):277driver.find_element(By.CLASS_NAME, "name_B")278279280@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")281@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")282@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")283def test_finding_asingle_element_by_empty_class_name_should_throw(driver, pages):284pages.load("xhtmlTest.html")285msg = r"\/errors#invalidselectorexception"286with pytest.raises(InvalidSelectorException, match=msg):287driver.find_element(By.CLASS_NAME, "")288289290@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")291@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")292@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")293def test_finding_multiple_elements_by_empty_class_name_should_throw(driver, pages):294pages.load("xhtmlTest.html")295with pytest.raises(InvalidSelectorException):296driver.find_elements(By.CLASS_NAME, "")297298299def test_finding_asingle_element_by_compound_class_name_should_throw(driver, pages):300pages.load("xhtmlTest.html")301with pytest.raises(NoSuchElementException):302driver.find_element(By.CLASS_NAME, "a b")303304305@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")306@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")307@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")308def test_finding_asingle_element_by_invalid_class_name_should_throw(driver, pages):309pages.load("xhtmlTest.html")310with pytest.raises(InvalidSelectorException):311driver.find_element(By.CLASS_NAME, "!@#$%^&*")312313314@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")315@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")316@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")317def test_finding_multiple_elements_by_invalid_class_name_should_throw(driver, pages):318pages.load("xhtmlTest.html")319with pytest.raises(InvalidSelectorException):320driver.find_elements(By.CLASS_NAME, "!@#$%^&*")321322323# By.xpath positive324325326def test_should_be_able_to_find_asingle_element_by_xpath(driver, pages):327pages.load("xhtmlTest.html")328element = driver.find_element(By.XPATH, "//h1")329assert element.text == "XHTML Might Be The Future"330331332def test_should_be_able_to_find_multiple_elements_by_xpath(driver, pages):333pages.load("xhtmlTest.html")334elements = driver.find_elements(By.XPATH, "//div")335assert len(elements) == 13336337338def test_should_be_able_to_find_many_elements_repeatedly_by_xpath(driver, pages):339pages.load("xhtmlTest.html")340xpath = "//node()[contains(@id,'id')]"341assert len(driver.find_elements(By.XPATH, xpath)) == 3342343xpath = "//node()[contains(@id,'nope')]"344assert len(driver.find_elements(By.XPATH, xpath)) == 0345346347def test_should_be_able_to_identify_elements_by_class(driver, pages):348pages.load("xhtmlTest.html")349header = driver.find_element(By.XPATH, "//h1[@class='header']")350assert header.text == "XHTML Might Be The Future"351352353def test_should_be_able_to_find_an_element_by_xpath_with_multiple_attributes(driver, pages):354pages.load("formPage.html")355element = driver.find_element(By.XPATH, "//form[@name='optional']/input[@type='submit' and @value='Click!']")356assert element.tag_name.lower() == "input"357assert element.get_attribute("value") == "Click!"358359360def test_finding_alink_by_xpath_should_locate_an_element_with_the_given_text(driver, pages):361pages.load("xhtmlTest.html")362element = driver.find_element(By.XPATH, "//a[text()='click me']")363assert element.text == "click me"364365366def test_finding_alink_by_xpath_using_contains_keyword_should_work(driver, pages):367pages.load("nestedElements.html")368element = driver.find_element(By.XPATH, "//a[contains(.,'hello world')]")369assert "hello world" in element.text370371372# @pytest.mark.xfail_chrome(raises=InvalidSelectorException)373# @pytest.mark.xfail_edge(raises=InvalidSelectorException)374# @pytest.mark.xfail_firefox(raises=InvalidSelectorException)375# @pytest.mark.xfail_remote(raises=InvalidSelectorException)376# @pytest.mark.xfail_safari(raises=NoSuchElementException)377# @pytest.mark.xfail_webkitgtk(raises=InvalidSelectorException)378# def test_Should_Be_Able_To_Find_Element_By_XPath_With_Namespace(driver, pages):379# pages.load("svgPage.html")380# element = driver.find_element(By.XPATH, "//svg:svg//svg:text")381# assert element.text == "Test Chart"382383384def test_should_be_able_to_find_element_by_xpath_in_xml_document(driver, pages):385pages.load("simple.xml")386element = driver.find_element(By.XPATH, "//foo")387assert "baz" in element.text388389390# By.xpath negative391392393def test_should_throw_an_exception_when_there_is_no_link_to_click(driver, pages):394pages.load("xhtmlTest.html")395with pytest.raises(NoSuchElementException):396driver.find_element(By.XPATH, "//a[@id='Not here']")397398399@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")400@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")401@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")402def test_should_throw_invalid_selector_exception_when_xpath_is_syntactically_invalid_in_driver_find_element(403driver, pages404):405pages.load("formPage.html")406with pytest.raises(InvalidSelectorException):407driver.find_element(By.XPATH, "this][isnot][valid")408409410@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")411@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")412@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")413def test_should_throw_invalid_selector_exception_when_xpath_is_syntactically_invalid_in_driver_find_elements(414driver, pages415):416pages.load("formPage.html")417with pytest.raises(InvalidSelectorException):418driver.find_elements(By.XPATH, "this][isnot][valid")419420421@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")422@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")423@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")424def test_should_throw_invalid_selector_exception_when_xpath_is_syntactically_invalid_in_element_find_element(425driver, pages426):427pages.load("formPage.html")428body = driver.find_element(By.TAG_NAME, "body")429with pytest.raises(InvalidSelectorException):430body.find_element(By.XPATH, "this][isnot][valid")431432433@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")434@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")435@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")436def test_should_throw_invalid_selector_exception_when_xpath_is_syntactically_invalid_in_element_find_elements(437driver, pages438):439pages.load("formPage.html")440body = driver.find_element(By.TAG_NAME, "body")441with pytest.raises(InvalidSelectorException):442body.find_elements(By.XPATH, "this][isnot][valid")443444445@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")446@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")447@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")448def test_should_throw_invalid_selector_exception_when_xpath_returns_wrong_type_in_driver_find_element(driver, pages):449pages.load("formPage.html")450with pytest.raises(InvalidSelectorException):451driver.find_element(By.XPATH, "count(//input)")452453454@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")455@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")456@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")457def test_should_throw_invalid_selector_exception_when_xpath_returns_wrong_type_in_driver_find_elements(driver, pages):458pages.load("formPage.html")459with pytest.raises(InvalidSelectorException):460driver.find_elements(By.XPATH, "count(//input)")461462463@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")464@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")465@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")466def test_should_throw_invalid_selector_exception_when_xpath_returns_wrong_type_in_element_find_element(driver, pages):467pages.load("formPage.html")468body = driver.find_element(By.TAG_NAME, "body")469with pytest.raises(InvalidSelectorException):470body.find_element(By.XPATH, "count(//input)")471472473@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")474@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")475@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")476def test_should_throw_invalid_selector_exception_when_xpath_returns_wrong_type_in_element_find_elements(driver, pages):477pages.load("formPage.html")478body = driver.find_element(By.TAG_NAME, "body")479with pytest.raises(InvalidSelectorException):480body.find_elements(By.XPATH, "count(//input)")481482483# By.css_Selector positive484485486def test_should_be_able_to_find_asingle_element_by_css_selector(driver, pages):487pages.load("xhtmlTest.html")488element = driver.find_element(By.CSS_SELECTOR, "div.content")489assert element.tag_name.lower() == "div"490assert element.get_attribute("class") == "content"491492493def test_should_be_able_to_find_multiple_elements_by_css_selector(driver, pages):494pages.load("xhtmlTest.html")495elements = driver.find_elements(By.CSS_SELECTOR, "p")496assert len(elements) > 1497498499def test_should_be_able_to_find_asingle_element_by_compound_css_selector(driver, pages):500pages.load("xhtmlTest.html")501element = driver.find_element(By.CSS_SELECTOR, "div.extraDiv, div.content")502assert element.tag_name.lower() == "div"503assert element.get_attribute("class") == "content"504505506def test_should_be_able_to_find_multiple_elements_by_compound_css_selector(driver, pages):507pages.load("xhtmlTest.html")508elements = driver.find_elements(By.CSS_SELECTOR, "div.extraDiv, div.content")509assert len(elements) > 1510assert elements[0].get_attribute("class") == "content"511assert elements[1].get_attribute("class") == "extraDiv"512513514def test_should_be_able_to_find_an_element_by_boolean_attribute_using_css_selector(driver, pages):515pages.load("locators_tests/boolean_attribute_selected.html")516element = driver.find_element(By.CSS_SELECTOR, "option[selected='selected']")517assert element.get_attribute("value") == "two"518519520def test_should_be_able_to_find_an_element_by_boolean_attribute_using_short_css_selector(driver, pages):521pages.load("locators_tests/boolean_attribute_selected.html")522element = driver.find_element(By.CSS_SELECTOR, "option[selected]")523assert element.get_attribute("value") == "two"524525526def test_should_be_able_to_find_an_element_by_boolean_attribute_using_short_css_selector_on_html_4_page(driver, pages):527pages.load("locators_tests/boolean_attribute_selected_html4.html")528element = driver.find_element(By.CSS_SELECTOR, "option[selected]")529assert element.get_attribute("value") == "two"530531532# By.css_Selector negative533534535def test_should_not_find_element_by_css_selector_when_there_is_no_such_element(driver, pages):536pages.load("xhtmlTest.html")537with pytest.raises(NoSuchElementException):538driver.find_element(By.CSS_SELECTOR, ".there-is-no-such-class")539540541def test_should_not_find_elements_by_css_selector_when_there_is_no_such_element(driver, pages):542pages.load("xhtmlTest.html")543elements = driver.find_elements(By.CSS_SELECTOR, ".there-is-no-such-class")544assert len(elements) == 0545546547@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")548@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")549@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")550def test_finding_asingle_element_by_empty_css_selector_should_throw(driver, pages):551pages.load("xhtmlTest.html")552with pytest.raises(InvalidSelectorException):553driver.find_element(By.CSS_SELECTOR, "")554555556@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")557@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")558@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")559def test_finding_multiple_elements_by_empty_css_selector_should_throw(driver, pages):560pages.load("xhtmlTest.html")561with pytest.raises(InvalidSelectorException):562driver.find_elements(By.CSS_SELECTOR, "")563564565@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")566@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")567@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")568def test_finding_asingle_element_by_invalid_css_selector_should_throw(driver, pages):569pages.load("xhtmlTest.html")570with pytest.raises(InvalidSelectorException):571driver.find_element(By.CSS_SELECTOR, "//a/b/c[@id='1']")572573574@pytest.mark.xfail_safari(reason="unlike chrome, safari raises TimeoutException")575@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")576@pytest.mark.xfail_edge(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")577def test_finding_multiple_elements_by_invalid_css_selector_should_throw(driver, pages):578pages.load("xhtmlTest.html")579with pytest.raises(InvalidSelectorException):580driver.find_elements(By.CSS_SELECTOR, "//a/b/c[@id='1']")581582583# By.link_Text positive584585586def test_should_be_able_to_find_alink_by_text(driver, pages):587pages.load("xhtmlTest.html")588link = driver.find_element(By.LINK_TEXT, "click me")589assert link.text == "click me"590591592def test_should_be_able_to_find_multiple_links_by_text(driver, pages):593pages.load("xhtmlTest.html")594elements = driver.find_elements(By.LINK_TEXT, "click me")595assert len(elements) == 2596597598def test_should_find_element_by_link_text_containing_equals_sign(driver, pages):599pages.load("xhtmlTest.html")600element = driver.find_element(By.LINK_TEXT, "Link=equalssign")601assert element.get_attribute("id") == "linkWithEqualsSign"602603604def test_should_find_multiple_elements_by_link_text_containing_equals_sign(driver, pages):605pages.load("xhtmlTest.html")606elements = driver.find_elements(By.LINK_TEXT, "Link=equalssign")607assert 1 == len(elements)608assert elements[0].get_attribute("id") == "linkWithEqualsSign"609610611def test_finds_by_link_text_on_xhtml_page(driver, pages):612pages.load("actualXhtmlPage.xhtml")613link_Text = "Foo"614element = driver.find_element(By.LINK_TEXT, link_Text)615assert element.text == link_Text616617618def test_link_with_formatting_tags(driver, pages):619pages.load("simpleTest.html")620elem = driver.find_element(By.ID, "links")621622res = elem.find_element(By.PARTIAL_LINK_TEXT, "link with formatting tags")623assert res.text == "link with formatting tags"624625626@pytest.mark.xfail_safari627def test_driver_can_get_link_by_link_test_ignoring_trailing_whitespace(driver, pages):628pages.load("simpleTest.html")629link = driver.find_element(By.LINK_TEXT, "link with trailing space")630assert link.get_attribute("id") == "linkWithTrailingSpace"631assert link.text == "link with trailing space"632633634# By.link_Text negative635636637def test_should_not_be_able_to_locate_by_link_text_asingle_element_that_does_not_exist(driver, pages):638pages.load("xhtmlTest.html")639with pytest.raises(NoSuchElementException):640driver.find_element(By.LINK_TEXT, "Not here either")641642643def test_should_not_be_able_to_locate_by_link_text_multiple_elements_that_do_not_exist(driver, pages):644pages.load("xhtmlTest.html")645elements = driver.find_elements(By.LINK_TEXT, "Not here either")646assert len(elements) == 0647648649# By.partial_Link_Text positive650651652def test_should_be_able_to_find_multiple_elements_by_partial_link_text(driver, pages):653pages.load("xhtmlTest.html")654elements = driver.find_elements(By.PARTIAL_LINK_TEXT, "ick me")655assert len(elements) == 2656657658def test_should_be_able_to_find_asingle_element_by_partial_link_text(driver, pages):659pages.load("xhtmlTest.html")660element = driver.find_element(By.PARTIAL_LINK_TEXT, "anon")661assert "anon" in element.text662663664def test_should_find_element_by_partial_link_text_containing_equals_sign(driver, pages):665pages.load("xhtmlTest.html")666element = driver.find_element(By.PARTIAL_LINK_TEXT, "Link=")667assert element.get_attribute("id") == "linkWithEqualsSign"668669670def test_should_find_multiple_elements_by_partial_link_text_containing_equals_sign(driver, pages):671pages.load("xhtmlTest.html")672elements = driver.find_elements(By.PARTIAL_LINK_TEXT, "Link=")673assert len(elements) == 1674assert elements[0].get_attribute("id") == "linkWithEqualsSign"675676677# Misc tests678679680def test_driver_should_be_able_to_find_elements_after_loading_more_than_one_page_at_atime(driver, pages):681pages.load("formPage.html")682pages.load("xhtmlTest.html")683link = driver.find_element(By.LINK_TEXT, "click me")684assert link.text == "click me"685686687# You don't want to ask why this is here688689690def test_when_finding_by_name_should_not_return_by_id(driver, pages):691pages.load("formPage.html")692693element = driver.find_element(By.NAME, "id-name1")694assert element.get_attribute("value") == "name"695696element = driver.find_element(By.ID, "id-name1")697assert element.get_attribute("value") == "id"698699element = driver.find_element(By.NAME, "id-name2")700assert element.get_attribute("value") == "name"701702element = driver.find_element(By.ID, "id-name2")703assert element.get_attribute("value") == "id"704705706def test_should_be_able_to_find_ahidden_elements_by_name(driver, pages):707pages.load("formPage.html")708element = driver.find_element(By.NAME, "hidden")709assert element.get_attribute("name") == "hidden"710711712def test_should_not_be_able_to_find_an_element_on_a_blank_page(driver, pages):713driver.get("about:blank")714with pytest.raises(NoSuchElementException):715driver.find_element(By.TAG_NAME, "a")716717718# custom finders tests719720721def test_register_and_get_custom_finder():722By.register_custom_finder("custom", "custom strategy")723assert By.get_finder("custom") == "custom strategy"724725726def test_get_nonexistent_finder():727assert By.get_finder("nonexistent") is None728729730def test_clear_custom_finders():731By.register_custom_finder("custom", "custom strategy")732By.clear_custom_finders()733assert By.get_finder("custom") is None734735736