Python Selenium Find Element by Name
Asked Answered
C

4

14

I am using Python / Selenium and am trying to select an input box that does not have an ID:

HTML

<div class="form-group">
    <input type="email"
           class="form-control"
           name="quantity"
           placeholder="Type a quantity">
</div>

Code

inputElement = driver.find_element_by_id("quantity")

What is the best way to select this element, by name?

Cordy answered 25/2, 2016 at 16:23 Comment(2)
Possibly for newer versions when using find_element_by_name (Pylint): "E1101: Instance of 'WebDriver' has no 'find_element_by_name' member (no-member)". For example, Selenium 4.12.0.Bakst
Related: Selenium - Python - AttributeError: 'WebDriver' object has no attribute 'find_element_by_name'. E.g., "Selenium just removed that method in version 4.3.0"Bakst
I
15

find_element_by_name() method would fit here:

driver.find_element_by_name("quantity")
Irisation answered 25/2, 2016 at 16:24 Comment(6)
The answer is almost in the title ;)Oriana
now this is deprecated, use ind_element(by=By.NAME, value=name) insteadCrenate
Typos are unhelpful. It is at least find_element(by=By.NAME, value=name)Bakst
Something like "from selenium.webdriver.common.by import By" is required for "By.NAME" to work.Bakst
A question about the import.Bakst
From an answer: "Do not add the "by=" and "value=" portion to the code."Bakst
S
7

In September 2022 with Selenium 4.4.0 the correct answer would be:

from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
inputElement = driver.find_element(by=By.NAME, value='quantity')
Shastashastra answered 1/9, 2022 at 17:16 Comment(3)
Yes, the "from selenium.webdriver.common.by import By" line is crucial. Otherwise the result may be (Pylint) "E0602: Undefined variable 'By' (undefined-variable)"Bakst
Though it does not work for a variable number of elements (dynamic content), say 0, 1, or 2: An exception is thrown, stopping the test run, if the element does not exist: "selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element"Bakst
And it, presumably, only returns the first element if there is more than one.Bakst
E
0

I have used this method to collect attributes from elements many times:

for i in browser.find_elements_by_class_name("form-control"):
    print i.get_attribute("name")
Equilibrium answered 25/2, 2016 at 16:36 Comment(0)
D
0

Preferable is to use explicit wait:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Chrome()
wait = WebDriverWait(driver, 5)
input_element = wait.until(EC.presence_of_element_located((By.NAME, 'quantity')))

More about waits here

Delirium answered 26/7, 2018 at 7:59 Comment(2)
Why is it preferable to use explicit wait?Bakst
Setting Explicit Wait is important in cases where there are certain elements that naturally take more time to load. If one sets an implicit wait command, then the browser will wait for the same time frame before loading every web element. This causes an unnecessary delay in executing the test script.Delirium

© 2022 - 2024 — McMap. All rights reserved.