Skip to content
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

Update python locators docs #1903

Open
wants to merge 8 commits into
base: trunk
Choose a base branch
from
Open
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
120 changes: 120 additions & 0 deletions examples/python/tests/elements/test_locators.py
Original file line number Diff line number Diff line change
@@ -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("[email protected]")

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()

Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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"));
Expand All @@ -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"));
Expand All @@ -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"));
Expand All @@ -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"));
Expand Down Expand Up @@ -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"));
Expand All @@ -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"));
Expand Down
Loading
Loading