Skip to content

moved python examples for Relative Locators #1962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions examples/python/tests/elements/test_locators.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,54 @@
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with


def testAbove():
driver = webdriver.Chrome()
driver.get("https://www.selenium.dev/selenium/web/inputs.html")

password_input = driver.find_element(By.NAME, "password-input")
email = locate_with(By.TAG_NAME, "input").above(password_input)
email.send_keys("test@test.com")

def testBelow():
driver = webdriver.Chrome()
driver.get("https://www.selenium.dev/selenium/web/inputs.html")

password_input = driver.find_element(By.NAME, "password_input")
search = locate_with(By.TAG_NAME, "input").below(password_input)
search.send_keys("XXXXXXXXXXXXX")

def testNear():
driver = webdriver.Chrome()
driver.get("https://www.selenium.dev/selenium/web/inputs.html")

week_input = driver.find_element(By.NAME, "week_input")
button = locate_with(By.TAG_NAME, "input").near(week_input)
button.click()

def testToTheLeftOf():
driver = webdriver.Chrome()
driver.get("https://www.selenium.dev/selenium/web/inputs.html")

submit_input = driver.find_element(By.NAME, "submit_input")
reset = locate_with(By.TAG_NAME, "input").to_the_left_of(submit_input)
reset.click()

def testToTheRightOf():
driver = webdriver.Chrome()
driver.get("https://www.selenium.dev/selenium/web/inputs.html")

reset_input = driver.find_element(By.NAME, "reset_input")
submit = locate_with(By.TAG_NAME, "input").to_the_right_of(reset_input)
submit.click()

def testAboveAndBelow():
driver = webdriver.Chrome()
driver.get("https://www.selenium.dev/selenium/web/inputs.html")

password_input = driver.find_element(By.NAME, "password_input")
number_input = driver.find_element(By.NAME, "number_input")
email = locate_with(By.TAG_NAME, "input").above(password_input).below(number_input)
email.send_keys("test@test.com")

Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ Let us consider the below example for understanding the relative locators.

{{< figure src="/images/documentation/webdriver/relative_locators.png" class="img-responsive text-center" alt="Relative Locators">}}


### Available relative locators

#### Above
Expand All @@ -450,12 +451,11 @@ If the email text field element is not easily identifiable for some reason, but
we can locate the text field element using the fact that it is an "input" element "above" the password element.

{{< tabpane langEqualsHeader=true >}}
{{< badge-examples >}}
{{< 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#L7-L12" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var emailLocator = RelativeBy.WithLocator(By.TagName("input")).Above(By.Id("password"));
Expand All @@ -481,8 +481,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#L15-L20" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var passwordLocator = RelativeBy.WithLocator(By.TagName("input")).Below(By.Id("email"));
Expand All @@ -508,8 +508,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#L31-L36" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var cancelLocator = RelativeBy.WithLocator(By.tagName("button")).LeftOf(By.Id("submit"));
Expand All @@ -535,8 +535,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#L39-L44" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var submitLocator = RelativeBy.WithLocator(By.tagName("button")).RightOf(By.Id("cancel"));
Expand All @@ -561,8 +561,8 @@ but its associated [input label element](https://developer.mozilla.org/en-US/doc

{{< tabpane langEqualsHeader=true >}}
{{< badge-examples >}}
{{< tab header="Java" >}}
By emailLocator = RelativeLocator.with(By.tagName("input")).near(By.id("lbl-email"));
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L23-L28" >}}
{{< /tab >}}
{{< tab header="Python" >}}
email_locator = locate_with(By.TAG_NAME, "input").near({By.ID: "lbl-email"})
Expand Down Expand Up @@ -590,8 +590,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#L47-L53" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var submitLocator = RelativeBy.WithLocator(By.tagName("button")).Below(By.Id("email")).RightOf(By.Id("cancel"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,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#L7-L12" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var emailLocator = RelativeBy.WithLocator(By.TagName("input")).Above(By.Id("password"));
Expand All @@ -468,11 +468,12 @@ If the password text field element is not easily identifiable for some reason, b
we can locate the text field element using the fact that it is an "input" element "below" the email element.

{{< tabpane langEqualsHeader=true >}}
{{< badge-examples >}}
{{< 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#L15-L20" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var passwordLocator = RelativeBy.WithLocator(By.TagName("input")).Below(By.Id("email"));
Expand All @@ -494,11 +495,12 @@ If the cancel button is not easily identifiable for some reason, but the submit
we can locate the cancel button element using the fact that it is a "button" element to the "left of" the submit element.

{{< tabpane langEqualsHeader=true >}}
{{< badge-examples >}}
{{< 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#L31-L36" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var cancelLocator = RelativeBy.WithLocator(By.tagName("button")).LeftOf(By.Id("submit"));
Expand All @@ -520,11 +522,12 @@ If the submit button is not easily identifiable for some reason, but the cancel
we can locate the submit button element using the fact that it is a "button" element "to the right of" the cancel element.

{{< tabpane langEqualsHeader=true >}}
{{< badge-examples >}}
{{< 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#L39-L44" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var submitLocator = RelativeBy.WithLocator(By.tagName("button")).RightOf(By.Id("cancel"));
Expand All @@ -548,8 +551,9 @@ One great use case for this is to work with a form element that doesn't have an
but its associated [input label element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label) does.

{{< tabpane langEqualsHeader=true >}}
{{< tab header="Java" >}}
By emailLocator = RelativeLocator.with(By.tagName("input")).near(By.id("lbl-email"));
{{< badge-examples >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L23-L28" >}}
{{< /tab >}}
{{< tab header="Python" >}}
email_locator = locate_with(By.TAG_NAME, "input").near({By.ID: "lbl-email"})
Expand All @@ -573,11 +577,12 @@ val emailLocator = RelativeLocator.with(By.tagName("input")).near(By.id("lbl-ema
You can also chain locators if needed. Sometimes the element is most easily identified as being both above/below one element and right/left of another.

{{< tabpane langEqualsHeader=true >}}
{{< badge-examples >}}
{{< 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#L47-L53" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var submitLocator = RelativeBy.WithLocator(By.tagName("button")).Below(By.Id("email")).RightOf(By.Id("cancel"));
Expand All @@ -591,4 +596,4 @@ let submitLocator = locateWith(By.tagName('button')).below(By.id('email')).toRig
{{< tab header="Kotlin" >}}
val submitLocator = RelativeLocator.with(By.tagName("button")).below(By.id("email")).toRightOf(By.id("cancel"))
{{< /tab >}}
{{< /tabpane >}}
{{< /tabpane >}}
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,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#L7-L12" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var emailLocator = RelativeBy.WithLocator(By.TagName("input")).Above(By.Id("password"));
Expand All @@ -471,11 +471,12 @@ If the password text field element is not easily identifiable for some reason, b
we can locate the text field element using the fact that it is an "input" element "below" the email element.

{{< tabpane langEqualsHeader=true >}}
{{< badge-examples >}}
{{< 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#L15-L20" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var passwordLocator = RelativeBy.WithLocator(By.TagName("input")).Below(By.Id("email"));
Expand All @@ -497,11 +498,12 @@ If the cancel button is not easily identifiable for some reason, but the submit
we can locate the cancel button element using the fact that it is a "button" element to the "left of" the submit element.

{{< tabpane langEqualsHeader=true >}}
{{< badge-examples >}}
{{< 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#L31-L36" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var cancelLocator = RelativeBy.WithLocator(By.tagName("button")).LeftOf(By.Id("submit"));
Expand All @@ -523,11 +525,12 @@ If the submit button is not easily identifiable for some reason, but the cancel
we can locate the submit button element using the fact that it is a "button" element "to the right of" the cancel element.

{{< tabpane langEqualsHeader=true >}}
{{< badge-examples >}}
{{< 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#L39-L44" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var submitLocator = RelativeBy.WithLocator(By.tagName("button")).RightOf(By.Id("cancel"));
Expand All @@ -551,8 +554,9 @@ One great use case for this is to work with a form element that doesn't have an
but its associated [input label element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label) does.

{{< tabpane langEqualsHeader=true >}}
{{< tab header="Java" >}}
By emailLocator = RelativeLocator.with(By.tagName("input")).near(By.id("lbl-email"));
{{< badge-examples >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="examples/python/tests/elements/test_locators.py#L23-L28" >}}
{{< /tab >}}
{{< tab header="Python" >}}
email_locator = locate_with(By.TAG_NAME, "input").near({By.ID: "lbl-email"})
Expand All @@ -576,11 +580,12 @@ val emailLocator = RelativeLocator.with(By.tagName("input")).near(By.id("lbl-ema
You can also chain locators if needed. Sometimes the element is most easily identified as being both above/below one element and right/left of another.

{{< tabpane langEqualsHeader=true >}}
{{< badge-examples >}}
{{< 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#L47-L53" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var submitLocator = RelativeBy.WithLocator(By.tagName("button")).Below(By.Id("email")).RightOf(By.Id("cancel"));
Expand All @@ -594,4 +599,4 @@ let submitLocator = locateWith(By.tagName('button')).below(By.id('email')).toRig
{{< tab header="Kotlin" >}}
val submitLocator = RelativeLocator.with(By.tagName("button")).below(By.id("email")).toRightOf(By.id("cancel"))
{{< /tab >}}
{{< /tabpane >}}
{{< /tabpane >}}
Loading