|
| 1 | +import pytest |
1 | 2 | from selenium import webdriver
|
| 3 | +from selenium.webdriver.common.by import By |
| 4 | +from selenium.webdriver.support.relative_locator import locate_with |
| 5 | + |
| 6 | +def test_find_by_classname(): |
| 7 | + driver = webdriver.Chrome() |
| 8 | + driver.get("https://www.selenium.dev/") |
| 9 | + |
| 10 | + driver.find_element(By.CLASS_NAME, "td-home") |
| 11 | + |
| 12 | + driver.quit() |
| 13 | + |
| 14 | +def test_find_by_css_selector(): |
| 15 | + driver = webdriver.Chrome() |
| 16 | + driver.get("https://www.selenium.dev/") |
| 17 | + |
| 18 | + driver.find_element(By.CSS_SELECTOR, "#announcement-banner") |
| 19 | + |
| 20 | + driver.quit() |
| 21 | + |
| 22 | +def test_find_by_id(): |
| 23 | + driver = webdriver.Chrome() |
| 24 | + driver.get("https://www.selenium.dev/") |
| 25 | + |
| 26 | + driver.find_element(By.ID, "announcement-banner") |
| 27 | + |
| 28 | + driver.quit() |
| 29 | + |
| 30 | +def test_find_by_name(): |
| 31 | + driver = webdriver.Chrome() |
| 32 | + driver.get("https://www.selenium.dev/selenium/web/formPage.html") |
| 33 | + |
| 34 | + driver.find_element(By.NAME, "image") |
| 35 | + |
| 36 | + driver.quit() |
| 37 | + |
| 38 | +def test_find_by_link_text(): |
| 39 | + driver = webdriver.Chrome() |
| 40 | + driver.get("https://www.selenium.dev/") |
| 41 | + |
| 42 | + driver.find_element(By.LINK_TEXT, "Documentation") |
| 43 | + |
| 44 | + driver.quit() |
| 45 | + |
| 46 | +def test_find_by_partial_link_text(): |
| 47 | + driver = webdriver.Chrome() |
| 48 | + driver.get("https://www.selenium.dev/documentation/") |
| 49 | + |
| 50 | + driver.find_element(By.PARTIAL_LINK_TEXT, "Selenium script") |
| 51 | + |
| 52 | + driver.quit() |
| 53 | + |
| 54 | +def test_find_by_tag_name(): |
| 55 | + driver = webdriver.Chrome() |
| 56 | + driver.get("https://www.selenium.dev/") |
| 57 | + |
| 58 | + driver.find_element(By.TAG_NAME, "nav") |
| 59 | + |
| 60 | + driver.quit() |
| 61 | + |
| 62 | +def test_find_by_xpath(): |
| 63 | + driver = webdriver.Chrome() |
| 64 | + driver.get("https://www.selenium.dev/") |
| 65 | + |
| 66 | + driver.find_element(By.XPATH, "//a[@class=\"navbar-brand\"]") |
| 67 | + |
| 68 | + driver.quit() |
| 69 | + |
| 70 | +pytest.mark.skip(reason='the examples are tied to an image with an example, on the site') |
| 71 | +def find_by_relative_locators(): |
| 72 | + driver = webdriver.Chrome() |
| 73 | + driver.get("https://www.selenium.dev/") |
| 74 | + |
| 75 | + email_locator = locate_with(By.TAG_NAME, "input").above({By.ID: "password"}) |
| 76 | + |
| 77 | + password_locator = locate_with(By.TAG_NAME, "input").below({By.ID: "email"}) |
| 78 | + |
| 79 | + cancel_locator = locate_with(By.TAG_NAME, "button").to_left_of({By.ID: "submit"}) |
| 80 | + |
| 81 | + submit_locator = locate_with(By.TAG_NAME, "button").to_right_of({By.ID: "cancel"}) |
| 82 | + |
| 83 | + email_locator = locate_with(By.TAG_NAME, "input").near({By.ID: "lbl-email"}) |
| 84 | + |
| 85 | + submit_locator = locate_with(By.TAG_NAME, "button").below({By.ID: "email"}).to_right_of({By.ID: "cancel"}) |
| 86 | + |
| 87 | + |
| 88 | +# def test_find_by_relative_locators(): |
| 89 | +# driver = webdriver.Chrome() |
| 90 | +# driver.get("https://www.selenium.dev/selenium/web/formPage.html") |
| 91 | + |
| 92 | +# locate_with(By.TAG_NAME, "input").above({ By.ID: "checkedchecky" }) |
| 93 | + |
| 94 | +# locate_with(By.TAG_NAME, "input").below({ By.ID: "checkedchecky" }) |
| 95 | + |
| 96 | +# locate_with(By.TAG_NAME, "select").to_left_of({ By.ID: "multi" }) |
| 97 | + |
| 98 | +# locate_with(By.TAG_NAME, "select").to_right_of({ By.NAME: "no-select" }) |
| 99 | + |
| 100 | +# locate_with(By.TAG_NAME, "p").near({By.ID: "lone_disabled_selected_radio"}) |
| 101 | + |
| 102 | +# locate_with(By.TAG_NAME, "select").to_right_of({ By.NAME: "no-select" }).below({ By.TAG_NAME: "form" }) |
| 103 | + |
| 104 | +# driver.quit() |
2 | 105 |
|
0 commit comments