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

Skip to content

Commit 3b13c2f

Browse files
authored
Updated find_element method (baijum#99)
1 parent 7a104ef commit 3b13c2f

File tree

3 files changed

+64
-66
lines changed

3 files changed

+64
-66
lines changed

source/getting-started.rst

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ Python like this.
1313

1414
from selenium import webdriver
1515
from selenium.webdriver.common.keys import Keys
16+
from selenium.webdriver.common.by import By
1617

1718
driver = webdriver.Firefox()
1819
driver.get("http://www.python.org")
1920
assert "Python" in driver.title
20-
elem = driver.find_element_by_name("q")
21+
elem = driver.find_element(By.NAME, "q")
2122
elem.clear()
2223
elem.send_keys("pycon")
2324
elem.send_keys(Keys.RETURN)
@@ -36,12 +37,14 @@ Example Explained
3637

3738
The `selenium.webdriver` module provides all the WebDriver implementations.
3839
Currently supported WebDriver implementations are Firefox, Chrome, IE and
39-
Remote. The `Keys` class provide keys in the keyboard like RETURN, F1, ALT etc.
40+
Remote. The `Keys` class provide keys in the keyboard like RETURN, F1, ALT etc.
41+
The `By` class is used to locate elements within a document.
4042

4143
::
4244

4345
from selenium import webdriver
4446
from selenium.webdriver.common.keys import Keys
47+
from selenium.webdriver.common.by import By
4548

4649
Next, the instance of Firefox WebDriver is created.
4750

@@ -61,13 +64,13 @@ The next line is an assertion to confirm that title has "Python" word in it::
6164

6265
assert "Python" in driver.title
6366

64-
WebDriver offers a number of ways to find elements using one of the
65-
`find_element_by_*` methods. For example, the input text element can be located
66-
by its `name` attribute using `find_element_by_name` method. A detailed
67-
explanation of finding elements is available in the :ref:`locating-elements`
67+
WebDriver offers a number of ways to find elements using the
68+
`find_element` method. For example, the input text element can be located
69+
by its `name` attribute using the `find_element` method and using By.NAME as its first parameter.
70+
A detailed explanation of finding elements is available in the :ref:`locating-elements`
6871
chapter::
6972

70-
elem = driver.find_element_by_name("q")
73+
elem = driver.find_element(By.NAME, "q")
7174

7275
Next, we are sending keys, this is similar to entering keys using your keyboard.
7376
Special keys can be sent using `Keys` class imported from
@@ -107,6 +110,7 @@ search functionality::
107110
import unittest
108111
from selenium import webdriver
109112
from selenium.webdriver.common.keys import Keys
113+
from selenium.webdriver.common.by import By
110114

111115
class PythonOrgSearch(unittest.TestCase):
112116

@@ -117,7 +121,7 @@ search functionality::
117121
driver = self.driver
118122
driver.get("http://www.python.org")
119123
self.assertIn("Python", driver.title)
120-
elem = driver.find_element_by_name("q")
124+
elem = driver.find_element(By.NAME, "q")
121125
elem.send_keys("pycon")
122126
elem.send_keys(Keys.RETURN)
123127
self.assertNotIn("No results found.", driver.page_source)
@@ -157,13 +161,14 @@ based on Java's JUnit. This module provides the framework for organizing the
157161
test cases. The `selenium.webdriver` module provides all the WebDriver
158162
implementations. Currently supported WebDriver implementations are Firefox,
159163
Chrome, IE and Remote. The `Keys` class provides keys in the keyboard like
160-
RETURN, F1, ALT etc.
164+
RETURN, F1, ALT etc. The `By` class is used to locate elements within a document.
161165

162166
::
163167

164168
import unittest
165169
from selenium import webdriver
166170
from selenium.webdriver.common.keys import Keys
171+
from selenium.webdriver.common.by import By
167172

168173
The test case class is inherited from `unittest.TestCase`. Inheriting from
169174
`TestCase` class is the way to tell `unittest` module that this is a test case::
@@ -202,13 +207,13 @@ The next line is an assertion to confirm that title has "Python" word in it::
202207
self.assertIn("Python", driver.title)
203208

204209

205-
WebDriver offers a number of ways to find elements using one of the
206-
`find_element_by_*` methods. For example, the input text element can be located
207-
by its `name` attribute using `find_element_by_name` method. Detailed
210+
WebDriver offers a number of ways to find elements using the
211+
`find_element` method. For example, the input text element can be located
212+
by its `name` attribute using the `find_element` method. Detailed
208213
explanation of finding elements is available in the :ref:`locating-elements`
209214
chapter::
210215

211-
elem = driver.find_element_by_name("q")
216+
elem = driver.find_element(By.NAME, "q")
212217

213218
Next, we are sending keys, this is similar to entering keys using your keyboard.
214219
Special keys can be send using `Keys` class imported from

source/locating-elements.rst

Lines changed: 35 additions & 42 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,18 +21,32 @@ 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"
28+
NAME = "name"
4929
XPATH = "xpath"
5030
LINK_TEXT = "link text"
5131
PARTIAL_LINK_TEXT = "partial link text"
52-
NAME = "name"
5332
TAG_NAME = "tag name"
5433
CLASS_NAME = "class name"
5534
CSS_SELECTOR = "css selector"
5635

36+
The 'By' class is used to specify which attribute is used to locate elements on a page.
37+
These are the various ways the attributes are used to locate elements on a page::
38+
39+
find_element(By.ID, "id")
40+
find_element(By.NAME, "name")
41+
find_element(By.XPATH, "xpath")
42+
find_element(By.LINK_TEXT, "link text")
43+
find_element(By.PARTIAL_LINK_TEXT, "partial link text")
44+
find_element(By.TAG_NAME, "tag name")
45+
find_element(By.CLASS_NAME, "class name")
46+
find_element(By.CSS_SELECTOR, "css selector")
47+
48+
If you want to locate several elements with the same attribute replace find_element with find_elements.
49+
5750

5851
Locating by Id
5952
~~~~~~~~~~~~~~
@@ -77,7 +70,7 @@ For instance, consider this page source::
7770

7871
The form element can be located like this::
7972

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

8275

8376
Locating by Name
@@ -103,12 +96,12 @@ For instance, consider this page source::
10396

10497
The username & password elements can be located like this::
10598

106-
username = driver.find_element_by_name('username')
107-
password = driver.find_element_by_name('password')
99+
username = driver.find_element(By.NAME, 'username')
100+
password = driver.find_element(By.NAME, 'password')
108101

109102
This will give the "Login" button as it occurs before the "Clear" button::
110103

111-
continue = driver.find_element_by_name('continue')
104+
continue = driver.find_element(By.NAME, 'continue')
112105

113106

114107
Locating by XPath
@@ -148,9 +141,9 @@ For instance, consider this page source::
148141

149142
The form elements can be located like this::
150143

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']")
144+
login_form = driver.find_element(By.XPATH, "/html/body/form[1]")
145+
login_form = driver.find_element(By.XPATH, "//form[1]")
146+
login_form = driver.find_element(By.XPATH, "//form[@id='loginForm']")
154147

155148

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

162155
The username element can be located like this::
163156

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']")
157+
username = driver.find_element(By.XPATH, "//form[input/@name='username']")
158+
username = driver.find_element(By.XPATH, "//form[@id='loginForm']/input[1]")
159+
username = driver.find_element(By.XPATH, "//input[@name='username']")
167160

168161
1. First form element with an input child element with `name` set to `username`
169162

@@ -174,8 +167,8 @@ The username element can be located like this::
174167

175168
The "Clear" button element can be located like this::
176169

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]")
170+
clear_button = driver.find_element(By.XPATH, "//input[@name='continue'][@type='button']")
171+
clear_button = driver.find_element(By.XPATH, "//form[@id='loginForm']/input[4]")
179172

180173

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

225218
The continue.html link can be located like this::
226219

227-
continue_link = driver.find_element_by_link_text('Continue')
228-
continue_link = driver.find_element_by_partial_link_text('Conti')
220+
continue_link = driver.find_element(By.LINK_TEXT, 'Continue')
221+
continue_link = driver.find_element(By.PARTIAL_LINK_TEXT, 'Conti')
229222

230223

231224
Locating Elements by Tag Name
@@ -246,7 +239,7 @@ For instance, consider this page source::
246239

247240
The heading (h1) element can be located like this::
248241

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

251244

252245
Locating Elements by Class Name
@@ -267,7 +260,7 @@ For instance, consider this page source::
267260

268261
The "p" element can be located like this::
269262

270-
content = driver.find_element_by_class_name('content')
263+
content = driver.find_element(By.CLASS_NAME, 'content')
271264

272265
Locating Elements by CSS Selectors
273266
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -288,7 +281,7 @@ For instance, consider this page source::
288281

289282
The "p" element can be located like this::
290283

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

293286
`Sauce Labs has good documentation
294287
<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)