Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9abdf45

Browse files
committed
Updated find_element method
1 parent be2535a commit 9abdf45

File tree

2 files changed

+31
-52
lines changed

2 files changed

+31
-52
lines changed

source/locating-elements.rst

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,13 @@ Locating Elements
44
-----------------
55

66
There are various strategies to locate elements in a page. You can use the most
7-
appropriate one for your case. Selenium provides the following methods to
7+
appropriate one for your case. Selenium provides the following method to
88
locate elements in a page:
99

10-
- `find_element_by_id`
11-
- `find_element_by_name`
12-
- `find_element_by_xpath`
13-
- `find_element_by_link_text`
14-
- `find_element_by_partial_link_text`
15-
- `find_element_by_tag_name`
16-
- `find_element_by_class_name`
17-
- `find_element_by_css_selector`
18-
10+
- `find_element`
1911

2012
**To find multiple elements (these methods will return a list):**
2113

22-
- `find_elements_by_name`
23-
- `find_elements_by_xpath`
24-
- `find_elements_by_link_text`
25-
- `find_elements_by_partial_link_text`
26-
- `find_elements_by_tag_name`
27-
- `find_elements_by_class_name`
28-
- `find_elements_by_css_selector`
29-
30-
31-
Apart from the public methods given above, there are two private methods which
32-
might be useful for locating page elements:
33-
34-
- `find_element`
3514
- `find_elements`
3615

3716

@@ -42,7 +21,7 @@ Example usage::
4221
driver.find_element(By.XPATH, '//button[text()="Some text"]')
4322
driver.find_elements(By.XPATH, '//button')
4423

45-
24+
The attributes available for the `By` class are used to locate elements on a page.
4625
These are the attributes available for `By` class::
4726

4827
ID = "id"
@@ -77,7 +56,7 @@ For instance, consider this page source::
7756

7857
The form element can be located like this::
7958

80-
login_form = driver.find_element_by_id('loginForm')
59+
login_form = driver.find_element(By.ID, 'loginForm')
8160

8261

8362
Locating by Name
@@ -103,12 +82,12 @@ For instance, consider this page source::
10382

10483
The username & password elements can be located like this::
10584

106-
username = driver.find_element_by_name('username')
107-
password = driver.find_element_by_name('password')
85+
username = driver.find_element(By.NAME, 'username')
86+
password = driver.find_element(By.NAME, 'password')
10887

10988
This will give the "Login" button as it occurs before the "Clear" button::
11089

111-
continue = driver.find_element_by_name('continue')
90+
continue = driver.find_element(By.NAME, 'continue')
11291

11392

11493
Locating by XPath
@@ -148,9 +127,9 @@ For instance, consider this page source::
148127

149128
The form elements can be located like this::
150129

151-
login_form = driver.find_element_by_xpath("/html/body/form[1]")
152-
login_form = driver.find_element_by_xpath("//form[1]")
153-
login_form = driver.find_element_by_xpath("//form[@id='loginForm']")
130+
login_form = driver.find_element(By.XPATH"/html/body/form[1]")
131+
login_form = driver.find_element(By.XPATH, "//form[1]")
132+
login_form = driver.find_element(By.XPATH, "//form[@id='loginForm']")
154133

155134

156135
1. Absolute path (would break if the HTML was changed only slightly)
@@ -161,9 +140,9 @@ The form elements can be located like this::
161140

162141
The username element can be located like this::
163142

164-
username = driver.find_element_by_xpath("//form[input/@name='username']")
165-
username = driver.find_element_by_xpath("//form[@id='loginForm']/input[1]")
166-
username = driver.find_element_by_xpath("//input[@name='username']")
143+
username = driver.find_element(By.XPATH, "//form[input/@name='username']")
144+
username = driver.find_element(By.XPATH, "//form[@id='loginForm']/input[1]")
145+
username = driver.find_element(By.XPATH, "//input[@name='username']")
167146

168147
1. First form element with an input child element with `name` set to `username`
169148

@@ -174,8 +153,8 @@ The username element can be located like this::
174153

175154
The "Clear" button element can be located like this::
176155

177-
clear_button = driver.find_element_by_xpath("//input[@name='continue'][@type='button']")
178-
clear_button = driver.find_element_by_xpath("//form[@id='loginForm']/input[4]")
156+
clear_button = driver.find_element(By.XPATH, "//input[@name='continue'][@type='button']")
157+
clear_button = driver.find_element(By.XPATH, "//form[@id='loginForm']/input[4]")
179158

180159

181160
1. Input with attribute `name` set to `continue` and attribute `type` set to
@@ -224,8 +203,8 @@ For instance, consider this page source::
224203

225204
The continue.html link can be located like this::
226205

227-
continue_link = driver.find_element_by_link_text('Continue')
228-
continue_link = driver.find_element_by_partial_link_text('Conti')
206+
continue_link = driver.find_element(By.LINK_TEXT, 'Continue')
207+
continue_link = driver.find_element(By.PARTIAL_LINK_TEXT, 'Conti')
229208

230209

231210
Locating Elements by Tag Name
@@ -246,7 +225,7 @@ For instance, consider this page source::
246225

247226
The heading (h1) element can be located like this::
248227

249-
heading1 = driver.find_element_by_tag_name('h1')
228+
heading1 = driver.find_element(By.TAG_NAME, 'h1')
250229

251230

252231
Locating Elements by Class Name
@@ -267,7 +246,7 @@ For instance, consider this page source::
267246

268247
The "p" element can be located like this::
269248

270-
content = driver.find_element_by_class_name('content')
249+
content = driver.find_element(By.CLASS_NAME, 'content')
271250

272251
Locating Elements by CSS Selectors
273252
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -288,7 +267,7 @@ For instance, consider this page source::
288267

289268
The "p" element can be located like this::
290269

291-
content = driver.find_element_by_css_selector('p.content')
270+
content = driver.find_element(By.CSS_SELECTOR, 'p.content')
292271

293272
`Sauce Labs has good documentation
294273
<https://saucelabs.com/resources/articles/selenium-tips-css-selectors>`_ on CSS

source/navigating.rst

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ ways to find elements. For example, given an element defined as::
2828

2929
you could find it using any of::
3030

31-
element = driver.find_element_by_id("passwd-id")
32-
element = driver.find_element_by_name("passwd")
33-
element = driver.find_element_by_xpath("//input[@id='passwd-id']")
34-
element = driver.find_element_by_css_selector("input#passwd-id")
31+
element = driver.find_element(By.ID, "passwd-id")
32+
element = driver.find_element(By.NAME, "passwd")
33+
element = driver.find_element(By.XPATH, "//input[@id='passwd-id']")
34+
element = driver.find_element(By.CSS_SELECTOR, "input#passwd-id")
3535

3636
You can also look for a link by its text, but be careful! The text must be an
3737
exact match! You should also be careful when using `XPATH in WebDriver`. If
@@ -74,8 +74,8 @@ about the other elements? You can "toggle" the state of the drop down, and you
7474
can use "setSelected" to set something like an `OPTION` tag selected. Dealing
7575
with `SELECT` tags isn't too bad::
7676

77-
element = driver.find_element_by_xpath("//select[@name='name']")
78-
all_options = element.find_elements_by_tag_name("option")
77+
element = driver.find_element(By.XPATH, "//select[@name='name']")
78+
all_options = element.find_elements(By.TAG_NAME, "option")
7979
for option in all_options:
8080
print("Value is: %s" % option.get_attribute("value"))
8181
option.click()
@@ -88,23 +88,23 @@ elements. WebDriver's support classes include one called a "Select", which
8888
provides useful methods for interacting with these::
8989

9090
from selenium.webdriver.support.ui import Select
91-
select = Select(driver.find_element_by_name('name'))
91+
select = Select(driver.find_element(By.NAME, 'name'))
9292
select.select_by_index(index)
9393
select.select_by_visible_text("text")
9494
select.select_by_value(value)
9595

9696

9797
WebDriver also provides features for deselecting all the selected options::
9898

99-
select = Select(driver.find_element_by_id('id'))
99+
select = Select(driver.find_element(By.ID, 'id'))
100100
select.deselect_all()
101101

102102
This will deselect all OPTIONs from that particular SELECT on the page.
103103

104104
Suppose in a test, we need the list of all default selected options, Select
105105
class provides a property method that returns a list::
106106

107-
select = Select(driver.find_element_by_xpath("//select[@name='name']"))
107+
select = Select(driver.find_element(By.XPATH, "//select[@name='name']"))
108108
all_selected_options = select.all_selected_options
109109
110110
To get all available options::
@@ -131,8 +131,8 @@ Drag and drop
131131
You can use drag and drop, either moving an element by a certain amount, or on
132132
to another element::
133133

134-
element = driver.find_element_by_name("source")
135-
target = driver.find_element_by_name("target")
134+
element = driver.find_element(By.NAME, "source")
135+
target = driver.find_element(By.NAME, "target")
136136

137137
from selenium.webdriver import ActionChains
138138
action_chains = ActionChains(driver)

0 commit comments

Comments
 (0)