3

I don't know the reason for this error, for some reason the find_element_by_() commands are not being recognized, I've already tried to reinstall everything, I changed the Python version, nothing works, does anyone know how to solve this problem?

Error: Unresolved attribute reference 'find_element_by_name' for class 'WebDriver

Prophet
  • 32,350
  • 22
  • 54
  • 79

3 Answers3

2

find_element_by_* methods are now deprecated.
find_element(By. are used now.
So, instead of find_element_by_name it is a driver.find_element(By.NAME, "name_attribute") now.
Similarly driver.find_element(By.XPATH, "element_xpath_locator") etc.
To use these methods you will need the following import:

from selenium.webdriver.common.by import By
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • Thanks, it worked, but why does the same code using find_element_by on another my other pc work normal? Could it be because of the Python version? In this one that gave a problem I'm using 3.7, on the other pc that works I use Python 3.9 – BOB CÃOMUNISTA Sep 02 '22 at 14:29
  • Probably you installed Selenium 4 on this PC. – Prophet Sep 02 '22 at 14:30
2

Make sure you import By from selenium

from selenium.webdriver.common.by import By

then you would format your code like this

dv.find_elements(By.NAME,"<enter name value>")
Rihhard
  • 66
  • 4
1

I don't know if this is the answer for python but in c# this how you do it

browser.FindElement(By.Name("NameofElement"));

Found this example for python

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element(By.NAME, "q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()

Here https://selenium-python.readthedocs.io/getting-started.html

Fredrik Norling
  • 3,461
  • 17
  • 21