diff --git a/examples/python/tests/elements/test_locators.py b/examples/python/tests/elements/test_locators.py index 53b695b6fc83..86f6c397f453 100644 --- a/examples/python/tests/elements/test_locators.py +++ b/examples/python/tests/elements/test_locators.py @@ -1,2 +1,122 @@ from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.support.relative_locator import locate_with + +def test_find_by_classname(): + driver = webdriver.Chrome() + driver.get("https://www.selenium.dev/") + + driver.find_element(By.CLASS_NAME, "td-home") + + driver.quit() + +def test_find_by_css_selector(): + driver = webdriver.Chrome() + driver.get("https://www.selenium.dev/documentation/") + + driver.find_element(By.CSS_SELECTOR, "#td-sidebar-menu") + + driver.quit() + +def test_find_by_id(): + driver = webdriver.Chrome() + driver.get("https://www.selenium.dev/documentation/") + + driver.find_element(By.ID, "td-sidebar-menu") + + driver.quit() + +def test_find_by_name(): + driver = webdriver.Chrome() + driver.get("https://www.selenium.dev/selenium/web/formPage.html") + + driver.find_element(By.NAME, "image") + + driver.quit() + +def test_find_by_link_text(): + driver = webdriver.Chrome() + driver.get("https://www.selenium.dev/") + + driver.find_element(By.LINK_TEXT, "Documentation") + + driver.quit() + +def test_find_by_partial_link_text(): + driver = webdriver.Chrome() + driver.get("https://www.selenium.dev/documentation/") + + driver.find_element(By.PARTIAL_LINK_TEXT, "Selenium script") + + driver.quit() + +def test_find_by_tag_name(): + driver = webdriver.Chrome() + driver.get("https://www.selenium.dev/") + + driver.find_element(By.TAG_NAME, "nav") + + driver.quit() + +def test_find_by_xpath(): + driver = webdriver.Chrome() + driver.get("https://www.selenium.dev/") + + driver.find_element(By.XPATH, "//a[@class=\"navbar-brand\"]") + + driver.quit() + +def test_relative_locators_above(): + driver = webdriver.Chrome() + driver.get("https://www.selenium.dev/selenium/web/inputs.html") + + email_input = driver.find_element(locate_with(By.TAG_NAME, "input").above({ By.NAME: "password_input" })) + email_input.send_keys("test@test.com") + + driver.quit() + +def test_relative_locators_below(): + driver = webdriver.Chrome() + driver.get("https://www.selenium.dev/selenium/web/inputs.html") + + password_input = driver.find_element(locate_with(By.TAG_NAME, "input").below({ By.NAME: "email_input" })) + password_input.send_keys("randompassword") + + driver.quit() + +def test_relative_locators_to_the_left_of(): + driver = webdriver.Chrome() + driver.get("https://www.selenium.dev/selenium/web/inputs.html") + + button = driver.find_element(locate_with(By.TAG_NAME, "input").to_left_of({ By.NAME: "submit_input" })) + button.click() + + driver.quit() + +def test_relative_locators_to_the_right_of(): + driver = webdriver.Chrome() + driver.get("https://www.selenium.dev/selenium/web/inputs.html") + + button = driver.find_element(locate_with(By.TAG_NAME, "input").to_right_of({ By.NAME: "reset_input" })) + button.click() + + driver.quit() + +def test_relative_locators_near(): + driver = webdriver.Chrome() + driver.get("https://www.selenium.dev/selenium/web/inputs.html") + + button = driver.find_element(locate_with(By.TAG_NAME, "input").near({ By.NAME: "week_input" })) + button.send_keys('someweek') + + driver.quit() + +def test_relative_locators_below_and_right_of(): + driver = webdriver.Chrome() + driver.get("https://www.selenium.dev/selenium/web/inputs.html") + + button = driver.find_element(locate_with(By.TAG_NAME, "input").below({ By.NAME: "week_input" }).to_right_of({ By.NAME: "button_input" })) + button.click() + + driver.quit() diff --git a/website_and_docs/content/documentation/webdriver/elements/locators.en.md b/website_and_docs/content/documentation/webdriver/elements/locators.en.md index d08963fd063d..ce003899dc0e 100644 --- a/website_and_docs/content/documentation/webdriver/elements/locators.en.md +++ b/website_and_docs/content/documentation/webdriver/elements/locators.en.md @@ -80,9 +80,8 @@ available in Selenium. WebDriver driver = new ChromeDriver(); driver.findElement(By.className("information")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.CLASS_NAME, "information") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L9" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -114,9 +113,8 @@ textbox, using css. WebDriver driver = new ChromeDriver(); driver.findElement(By.cssSelector("#fname")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.CSS_SELECTOR, "#fname") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L17" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -146,9 +144,8 @@ We will identify the Last Name field using it. WebDriver driver = new ChromeDriver(); driver.findElement(By.id("lname")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.ID, "lname") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L25" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -179,9 +176,8 @@ We will identify the Newsletter checkbox using it. WebDriver driver = new ChromeDriver(); driver.findElement(By.name("newsletter")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.NAME, "newsletter") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L33" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -210,9 +206,8 @@ In the HTML snippet shared, we have a link available, let's see how will we loca WebDriver driver = new ChromeDriver(); driver.findElement(By.linkText("Selenium Official Page")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.LINK_TEXT, "Selenium Official Page") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L41" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -242,9 +237,8 @@ In the HTML snippet shared, we have a link available, lets see how will we locat WebDriver driver = new ChromeDriver(); driver.findElement(By.partialLinkText("Official Page")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.PARTIAL_LINK_TEXT, "Official Page") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L49" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -272,9 +266,8 @@ From the above HTML snippet shared, lets identify the link, using its html tag " WebDriver driver = new ChromeDriver(); driver.findElement(By.tagName("a")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.TAG_NAME, "a") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L57" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -308,9 +301,8 @@ first name text box. Let us create locator for female radio button using xpath. WebDriver driver = new ChromeDriver(); driver.findElement(By.xpath("//input[@value='f']")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.XPATH, "//input[@value='f']") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L65" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -345,10 +337,8 @@ others it's as simple as setting a parameter in the FindElement function WebDriver driver = new ChromeDriver(); driver.findElement(By.className("information")); {{< /tab >}} - {{< tab header="Python" >}} - from selenium.webdriver.common.by import By - driver = webdriver.Chrome() - driver.find_element(By.CLASS_NAME, "information") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L9" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -454,8 +444,8 @@ we can locate the text field element using the fact that it is an "input" elemen {{< tab header="Java" >}} By emailLocator = RelativeLocator.with(By.tagName("input")).above(By.id("password")); {{< /tab >}} -{{< tab header="Python" >}} -email_locator = locate_with(By.TAG_NAME, "input").above({By.ID: "password"}) +{{< tab header="Python" text=true >}} +{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L73" >}} {{< /tab >}} {{< tab header="CSharp" >}} var emailLocator = RelativeBy.WithLocator(By.TagName("input")).Above(By.Id("password")); @@ -481,8 +471,8 @@ we can locate the text field element using the fact that it is an "input" elemen {{< tab header="Java" >}} By passwordLocator = RelativeLocator.with(By.tagName("input")).below(By.id("email")); {{< /tab >}} -{{< tab header="Python" >}} -password_locator = locate_with(By.TAG_NAME, "input").below({By.ID: "email"}) +{{< tab header="Python" text=true >}} +{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L82" >}} {{< /tab >}} {{< tab header="CSharp" >}} var passwordLocator = RelativeBy.WithLocator(By.TagName("input")).Below(By.Id("email")); @@ -508,8 +498,8 @@ we can locate the cancel button element using the fact that it is a "button" ele {{< tab header="Java" >}} By cancelLocator = RelativeLocator.with(By.tagName("button")).toLeftOf(By.id("submit")); {{< /tab >}} -{{< tab header="Python" >}} -cancel_locator = locate_with(By.TAG_NAME, "button").to_left_of({By.ID: "submit"}) +{{< tab header="Python" text=true >}} +{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L91" >}} {{< /tab >}} {{< tab header="CSharp" >}} var cancelLocator = RelativeBy.WithLocator(By.tagName("button")).LeftOf(By.Id("submit")); @@ -535,8 +525,8 @@ we can locate the submit button element using the fact that it is a "button" ele {{< tab header="Java" >}} By submitLocator = RelativeLocator.with(By.tagName("button")).toRightOf(By.id("cancel")); {{< /tab >}} -{{< tab header="Python" >}} -submit_locator = locate_with(By.TAG_NAME, "button").to_right_of({By.ID: "cancel"}) +{{< tab header="Python" text=true >}} +{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L100" >}} {{< /tab >}} {{< tab header="CSharp" >}} var submitLocator = RelativeBy.WithLocator(By.tagName("button")).RightOf(By.Id("cancel")); @@ -564,8 +554,8 @@ but its associated [input label element](https://developer.mozilla.org/en-US/doc {{< tab header="Java" >}} By emailLocator = RelativeLocator.with(By.tagName("input")).near(By.id("lbl-email")); {{< /tab >}} -{{< tab header="Python" >}} -email_locator = locate_with(By.TAG_NAME, "input").near({By.ID: "lbl-email"}) +{{< tab header="Python" text=true >}} +{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L109" >}} {{< /tab >}} {{< tab header="CSharp" >}} var emailLocator = RelativeBy.WithLocator(By.tagName("input")).Near(By.Id("lbl-email")); @@ -590,8 +580,8 @@ You can also chain locators if needed. Sometimes the element is most easily iden {{< tab header="Java" >}} By submitLocator = RelativeLocator.with(By.tagName("button")).below(By.id("email")).toRightOf(By.id("cancel")); {{< /tab >}} -{{< tab header="Python" >}} -submit_locator = locate_with(By.TAG_NAME, "button").below({By.ID: "email"}).to_right_of({By.ID: "cancel"}) +{{< tab header="Python" text=true >}} +{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L118" >}} {{< /tab >}} {{< tab header="CSharp" >}} var submitLocator = RelativeBy.WithLocator(By.tagName("button")).Below(By.Id("email")).RightOf(By.Id("cancel")); diff --git a/website_and_docs/content/documentation/webdriver/elements/locators.ja.md b/website_and_docs/content/documentation/webdriver/elements/locators.ja.md index 28b11bcd66cc..ad6d940f828e 100644 --- a/website_and_docs/content/documentation/webdriver/elements/locators.ja.md +++ b/website_and_docs/content/documentation/webdriver/elements/locators.ja.md @@ -78,9 +78,8 @@ available in Selenium. WebDriver driver = new ChromeDriver(); driver.findElement(By.className("information")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.CLASS_NAME, "information") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L9" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -111,9 +110,8 @@ textbox, using css. WebDriver driver = new ChromeDriver(); driver.findElement(By.cssSelector("#fname")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.CSS_SELECTOR, "#fname") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L17" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -142,9 +140,8 @@ We will identify the Last Name field using it. WebDriver driver = new ChromeDriver(); driver.findElement(By.id("lname")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.ID, "lname") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L25" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -174,9 +171,8 @@ We will identify the Newsletter checkbox using it. WebDriver driver = new ChromeDriver(); driver.findElement(By.name("newsletter")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.NAME, "newsletter") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L33" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -204,9 +200,8 @@ In the HTML snippet shared, we have a link available, lets see how will we locat WebDriver driver = new ChromeDriver(); driver.findElement(By.linkText("Selenium Official Page")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.LINK_TEXT, "Selenium Official Page") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L41" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -235,9 +230,8 @@ In the HTML snippet shared, we have a link available, lets see how will we locat WebDriver driver = new ChromeDriver(); driver.findElement(By.partialLinkText("Official Page")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.PARTIAL_LINK_TEXT, "Official Page") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L49" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -264,9 +258,8 @@ From the above HTML snippet shared, lets identify the link, using its html tag " WebDriver driver = new ChromeDriver(); driver.findElement(By.tagName("a")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.TAG_NAME, "a") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L57" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -299,9 +292,8 @@ first name text box. Let us create locator for female radio button using xpath. WebDriver driver = new ChromeDriver(); driver.findElement(By.xpath("//input[@value='f']")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.XPATH, "//input[@value='f']") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L65" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -335,10 +327,8 @@ others it's as simple as setting a parameter in the FindElement function WebDriver driver = new ChromeDriver(); driver.findElement(By.className("information")); {{< /tab >}} - {{< tab header="Python" >}} - from selenium.webdriver.common.by import By - driver = webdriver.Chrome() - driver.find_element(By.CLASS_NAME, "information") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L9" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -445,8 +435,8 @@ we can locate the text field element using the fact that it is an "input" elemen {{< tab header="Java" >}} By emailLocator = RelativeLocator.with(By.tagName("input")).above(By.id("password")); {{< /tab >}} -{{< tab header="Python" >}} -email_locator = locate_with(By.TAG_NAME, "input").above({By.ID: "password"}) +{{< tab header="Python" text=true >}} +{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L73" >}} {{< /tab >}} {{< tab header="CSharp" >}} var emailLocator = RelativeBy.WithLocator(By.TagName("input")).Above(By.Id("password")); @@ -471,8 +461,8 @@ we can locate the text field element using the fact that it is an "input" elemen {{< tab header="Java" >}} By passwordLocator = RelativeLocator.with(By.tagName("input")).below(By.id("email")); {{< /tab >}} -{{< tab header="Python" >}} -password_locator = locate_with(By.TAG_NAME, "input").below({By.ID: "email"}) +{{< tab header="Python" text=true >}} +{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L82" >}} {{< /tab >}} {{< tab header="CSharp" >}} var passwordLocator = RelativeBy.WithLocator(By.TagName("input")).Below(By.Id("email")); @@ -497,8 +487,8 @@ we can locate the cancel button element using the fact that it is a "button" ele {{< tab header="Java" >}} By cancelLocator = RelativeLocator.with(By.tagName("button")).toLeftOf(By.id("submit")); {{< /tab >}} -{{< tab header="Python" >}} -cancel_locator = locate_with(By.TAG_NAME, "button").to_left_of({By.ID: "submit"}) +{{< tab header="Python" text=true >}} +{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L91" >}} {{< /tab >}} {{< tab header="CSharp" >}} var cancelLocator = RelativeBy.WithLocator(By.tagName("button")).LeftOf(By.Id("submit")); @@ -523,8 +513,8 @@ we can locate the submit button element using the fact that it is a "button" ele {{< tab header="Java" >}} By submitLocator = RelativeLocator.with(By.tagName("button")).toRightOf(By.id("cancel")); {{< /tab >}} -{{< tab header="Python" >}} -submit_locator = locate_with(By.TAG_NAME, "button").to_right_of({By.ID: "cancel"}) +{{< tab header="Python" text=true >}} +{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L100" >}} {{< /tab >}} {{< tab header="CSharp" >}} var submitLocator = RelativeBy.WithLocator(By.tagName("button")).RightOf(By.Id("cancel")); @@ -551,8 +541,8 @@ but its associated [input label element](https://developer.mozilla.org/en-US/doc {{< tab header="Java" >}} By emailLocator = RelativeLocator.with(By.tagName("input")).near(By.id("lbl-email")); {{< /tab >}} -{{< tab header="Python" >}} -email_locator = locate_with(By.TAG_NAME, "input").near({By.ID: "lbl-email"}) +{{< tab header="Python" text=true >}} +{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L109" >}} {{< /tab >}} {{< tab header="CSharp" >}} var emailLocator = RelativeBy.WithLocator(By.tagName("input")).Near(By.Id("lbl-email")); @@ -576,8 +566,8 @@ You can also chain locators if needed. Sometimes the element is most easily iden {{< tab header="Java" >}} By submitLocator = RelativeLocator.with(By.tagName("button")).below(By.id("email")).toRightOf(By.id("cancel")); {{< /tab >}} -{{< tab header="Python" >}} -submit_locator = locate_with(By.TAG_NAME, "button").below({By.ID: "email"}).to_right_of({By.ID: "cancel"}) +{{< tab header="Python" text=true >}} +{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L118" >}} {{< /tab >}} {{< tab header="CSharp" >}} var submitLocator = RelativeBy.WithLocator(By.tagName("button")).Below(By.Id("email")).RightOf(By.Id("cancel")); diff --git a/website_and_docs/content/documentation/webdriver/elements/locators.pt-br.md b/website_and_docs/content/documentation/webdriver/elements/locators.pt-br.md index de57a4812d37..04e5e60fe909 100644 --- a/website_and_docs/content/documentation/webdriver/elements/locators.pt-br.md +++ b/website_and_docs/content/documentation/webdriver/elements/locators.pt-br.md @@ -81,9 +81,8 @@ available in Selenium. WebDriver driver = new ChromeDriver(); driver.findElement(By.className("information")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.CLASS_NAME, "information") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L9" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -114,9 +113,8 @@ textbox, using css. WebDriver driver = new ChromeDriver(); driver.findElement(By.cssSelector("#fname")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.CSS_SELECTOR, "#fname") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L17" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -145,9 +143,8 @@ We will identify the Last Name field using it. WebDriver driver = new ChromeDriver(); driver.findElement(By.id("lname")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.ID, "lname") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L25" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -177,9 +174,8 @@ We will identify the Newsletter checkbox using it. WebDriver driver = new ChromeDriver(); driver.findElement(By.name("newsletter")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.NAME, "newsletter") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L33" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -207,9 +203,8 @@ In the HTML snippet shared, we have a link available, lets see how will we locat WebDriver driver = new ChromeDriver(); driver.findElement(By.linkText("Selenium Official Page")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.LINK_TEXT, "Selenium Official Page") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L41" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -238,9 +233,8 @@ In the HTML snippet shared, we have a link available, lets see how will we locat WebDriver driver = new ChromeDriver(); driver.findElement(By.partialLinkText("Official Page")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.PARTIAL_LINK_TEXT, "Official Page") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L49" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -267,9 +261,8 @@ From the above HTML snippet shared, lets identify the link, using its html tag " WebDriver driver = new ChromeDriver(); driver.findElement(By.tagName("a")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.TAG_NAME, "a") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L57" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -302,9 +295,8 @@ first name text box. Let us create locator for female radio button using xpath. WebDriver driver = new ChromeDriver(); driver.findElement(By.xpath("//input[@value='f']")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.XPATH, "//input[@value='f']") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L65" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -338,10 +330,8 @@ others it's as simple as setting a parameter in the FindElement function WebDriver driver = new ChromeDriver(); driver.findElement(By.className("information")); {{< /tab >}} - {{< tab header="Python" >}} - from selenium.webdriver.common.by import By - driver = webdriver.Chrome() - driver.find_element(By.CLASS_NAME, "information") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L9" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -448,8 +438,8 @@ we can locate the text field element using the fact that it is an "input" elemen {{< tab header="Java" >}} By emailLocator = RelativeLocator.with(By.tagName("input")).above(By.id("password")); {{< /tab >}} -{{< tab header="Python" >}} -email_locator = locate_with(By.TAG_NAME, "input").above({By.ID: "password"}) +{{< tab header="Python" text=true >}} +{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L73" >}} {{< /tab >}} {{< tab header="CSharp" >}} var emailLocator = RelativeBy.WithLocator(By.TagName("input")).Above(By.Id("password")); @@ -474,8 +464,8 @@ we can locate the text field element using the fact that it is an "input" elemen {{< tab header="Java" >}} By passwordLocator = RelativeLocator.with(By.tagName("input")).below(By.id("email")); {{< /tab >}} -{{< tab header="Python" >}} -password_locator = locate_with(By.TAG_NAME, "input").below({By.ID: "email"}) +{{< tab header="Python" text=true >}} +{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L82" >}} {{< /tab >}} {{< tab header="CSharp" >}} var passwordLocator = RelativeBy.WithLocator(By.TagName("input")).Below(By.Id("email")); @@ -500,8 +490,8 @@ we can locate the cancel button element using the fact that it is a "button" ele {{< tab header="Java" >}} By cancelLocator = RelativeLocator.with(By.tagName("button")).toLeftOf(By.id("submit")); {{< /tab >}} -{{< tab header="Python" >}} -cancel_locator = locate_with(By.TAG_NAME, "button").to_left_of({By.ID: "submit"}) +{{< tab header="Python" text=true >}} +{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L91" >}} {{< /tab >}} {{< tab header="CSharp" >}} var cancelLocator = RelativeBy.WithLocator(By.tagName("button")).LeftOf(By.Id("submit")); @@ -526,8 +516,8 @@ we can locate the submit button element using the fact that it is a "button" ele {{< tab header="Java" >}} By submitLocator = RelativeLocator.with(By.tagName("button")).toRightOf(By.id("cancel")); {{< /tab >}} -{{< tab header="Python" >}} -submit_locator = locate_with(By.TAG_NAME, "button").to_right_of({By.ID: "cancel"}) +{{< tab header="Python" text=true >}} +{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L100" >}} {{< /tab >}} {{< tab header="CSharp" >}} var submitLocator = RelativeBy.WithLocator(By.tagName("button")).RightOf(By.Id("cancel")); @@ -554,8 +544,8 @@ but its associated [input label element](https://developer.mozilla.org/en-US/doc {{< tab header="Java" >}} By emailLocator = RelativeLocator.with(By.tagName("input")).near(By.id("lbl-email")); {{< /tab >}} -{{< tab header="Python" >}} -email_locator = locate_with(By.TAG_NAME, "input").near({By.ID: "lbl-email"}) +{{< tab header="Python" text=true >}} +{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L109" >}} {{< /tab >}} {{< tab header="CSharp" >}} var emailLocator = RelativeBy.WithLocator(By.tagName("input")).Near(By.Id("lbl-email")); @@ -579,8 +569,8 @@ You can also chain locators if needed. Sometimes the element is most easily iden {{< tab header="Java" >}} By submitLocator = RelativeLocator.with(By.tagName("button")).below(By.id("email")).toRightOf(By.id("cancel")); {{< /tab >}} -{{< tab header="Python" >}} -submit_locator = locate_with(By.TAG_NAME, "button").below({By.ID: "email"}).to_right_of({By.ID: "cancel"}) +{{< tab header="Python" text=true >}} +{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L118" >}} {{< /tab >}} {{< tab header="CSharp" >}} var submitLocator = RelativeBy.WithLocator(By.tagName("button")).Below(By.Id("email")).RightOf(By.Id("cancel")); diff --git a/website_and_docs/content/documentation/webdriver/elements/locators.zh-cn.md b/website_and_docs/content/documentation/webdriver/elements/locators.zh-cn.md index b8e38f81faff..d5288f7dd86d 100644 --- a/website_and_docs/content/documentation/webdriver/elements/locators.zh-cn.md +++ b/website_and_docs/content/documentation/webdriver/elements/locators.zh-cn.md @@ -81,9 +81,8 @@ available in Selenium. WebDriver driver = new ChromeDriver(); driver.findElement(By.className("information")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.CLASS_NAME, "information") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L9" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -114,9 +113,8 @@ textbox, using css. WebDriver driver = new ChromeDriver(); driver.findElement(By.cssSelector("#fname")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.CSS_SELECTOR, "#fname") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L17" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -145,9 +143,8 @@ We will identify the Last Name field using it. WebDriver driver = new ChromeDriver(); driver.findElement(By.id("lname")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.ID, "lname") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L25" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -177,9 +174,8 @@ We will identify the Newsletter checkbox using it. WebDriver driver = new ChromeDriver(); driver.findElement(By.name("newsletter")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.NAME, "newsletter") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L33" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -207,9 +203,8 @@ In the HTML snippet shared, we have a link available, lets see how will we locat WebDriver driver = new ChromeDriver(); driver.findElement(By.linkText("Selenium Official Page")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.LINK_TEXT, "Selenium Official Page") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L41" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -238,9 +233,8 @@ In the HTML snippet shared, we have a link available, lets see how will we locat WebDriver driver = new ChromeDriver(); driver.findElement(By.partialLinkText("Official Page")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.PARTIAL_LINK_TEXT, "Official Page") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L49" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -267,9 +261,8 @@ From the above HTML snippet shared, lets identify the link, using its html tag " WebDriver driver = new ChromeDriver(); driver.findElement(By.tagName("a")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.TAG_NAME, "a") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L57" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -302,9 +295,8 @@ first name text box. Let us create locator for female radio button using xpath. WebDriver driver = new ChromeDriver(); driver.findElement(By.xpath("//input[@value='f']")); {{< /tab >}} - {{< tab header="Python" >}} - driver = webdriver.Chrome() - driver.find_element(By.XPATH, "//input[@value='f']") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L65" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -338,10 +330,8 @@ others it's as simple as setting a parameter in the FindElement function WebDriver driver = new ChromeDriver(); driver.findElement(By.className("information")); {{< /tab >}} - {{< tab header="Python" >}} - from selenium.webdriver.common.by import By - driver = webdriver.Chrome() - driver.find_element(By.CLASS_NAME, "information") + {{< tab header="Python" text=true >}} + {{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L9" >}} {{< /tab >}} {{< tab header="CSharp" >}} var driver = new ChromeDriver(); @@ -448,8 +438,8 @@ we can locate the text field element using the fact that it is an "input" elemen {{< tab header="Java" >}} By emailLocator = RelativeLocator.with(By.tagName("input")).above(By.id("password")); {{< /tab >}} -{{< tab header="Python" >}} -email_locator = locate_with(By.TAG_NAME, "input").above({By.ID: "password"}) +{{< tab header="Python" text=true >}} +{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L73" >}} {{< /tab >}} {{< tab header="CSharp" >}} var emailLocator = RelativeBy.WithLocator(By.TagName("input")).Above(By.Id("password")); @@ -474,8 +464,8 @@ we can locate the text field element using the fact that it is an "input" elemen {{< tab header="Java" >}} By passwordLocator = RelativeLocator.with(By.tagName("input")).below(By.id("email")); {{< /tab >}} -{{< tab header="Python" >}} -password_locator = locate_with(By.TAG_NAME, "input").below({By.ID: "email"}) +{{< tab header="Python" text=true >}} +{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L82" >}} {{< /tab >}} {{< tab header="CSharp" >}} var passwordLocator = RelativeBy.WithLocator(By.TagName("input")).Below(By.Id("email")); @@ -500,8 +490,8 @@ we can locate the cancel button element using the fact that it is a "button" ele {{< tab header="Java" >}} By cancelLocator = RelativeLocator.with(By.tagName("button")).toLeftOf(By.id("submit")); {{< /tab >}} -{{< tab header="Python" >}} -cancel_locator = locate_with(By.TAG_NAME, "button").to_left_of({By.ID: "submit"}) +{{< tab header="Python" text=true >}} +{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L91" >}} {{< /tab >}} {{< tab header="CSharp" >}} var cancelLocator = RelativeBy.WithLocator(By.tagName("button")).LeftOf(By.Id("submit")); @@ -526,8 +516,8 @@ we can locate the submit button element using the fact that it is a "button" ele {{< tab header="Java" >}} By submitLocator = RelativeLocator.with(By.tagName("button")).toRightOf(By.id("cancel")); {{< /tab >}} -{{< tab header="Python" >}} -submit_locator = locate_with(By.TAG_NAME, "button").to_right_of({By.ID: "cancel"}) +{{< tab header="Python" text=true >}} +{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L100" >}} {{< /tab >}} {{< tab header="CSharp" >}} var submitLocator = RelativeBy.WithLocator(By.tagName("button")).RightOf(By.Id("cancel")); @@ -554,8 +544,8 @@ but its associated [input label element](https://developer.mozilla.org/en-US/doc {{< tab header="Java" >}} By emailLocator = RelativeLocator.with(By.tagName("input")).near(By.id("lbl-email")); {{< /tab >}} -{{< tab header="Python" >}} -email_locator = locate_with(By.TAG_NAME, "input").near({By.ID: "lbl-email"}) +{{< tab header="Python" text=true >}} +{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L109" >}} {{< /tab >}} {{< tab header="CSharp" >}} var emailLocator = RelativeBy.WithLocator(By.tagName("input")).Near(By.Id("lbl-email")); @@ -579,8 +569,8 @@ You can also chain locators if needed. Sometimes the element is most easily iden {{< tab header="Java" >}} By submitLocator = RelativeLocator.with(By.tagName("button")).below(By.id("email")).toRightOf(By.id("cancel")); {{< /tab >}} -{{< tab header="Python" >}} -submit_locator = locate_with(By.TAG_NAME, "button").below({By.ID: "email"}).to_right_of({By.ID: "cancel"}) +{{< tab header="Python" text=true >}} +{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L118" >}} {{< /tab >}} {{< tab header="CSharp" >}} var submitLocator = RelativeBy.WithLocator(By.tagName("button")).Below(By.Id("email")).RightOf(By.Id("cancel"));