Confused by Python find_element_by_id Error

Question:

This should be a seemingly simple problem-

Python v3.10.6, Selenium v4.7.2

I’m attempting the most basic of webcrawling activities…but find_element_by_id (or any other find element methods for that matter) are returning a "AttributeError: ‘WebDriver’ object has no attribute ‘find_element_by_id’" error

Trying to (at first) just click the help botton at https://www.mouser.com/
Am getting that element ID as "aHelp":

Help Button ID

The code:

Code

I always get this Attribute Error…
Error

Asked By: 86Meesta2

||

Answers:

Selenium’s newer versions no longer support using driver.find_element_by_*.
try driver.find_element(By.ID, "the id you want")
for your example :

from selenium import webdriver
import time
from selenium.webdriver.common.by import By
driver = webdriver.Chrome("chromedriver.exe")
driver.get("https://www.mouser.com/")
time.sleep(2)
driver.find_element(By.ID,"aHelp").click()

Also for the rest of instructions(for instance XPath , …) see this link

Answered By: Nameless

You should do

from selenium.webdriver.common.by import By

driver.find_element(By.ID, 'aHelp').click()

See https://selenium-python.readthedocs.io/locating-elements.html for details.

Answered By: Diego Torres Milano