Home > Mobile >  Confused by Python find_element_by_id Error
Confused by Python find_element_by_id Error

Time:01-07

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 Help Button ID

The code:

Code

I always get this Attribute Error... Error

CodePudding user response:

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.

CodePudding user response:

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

  • Related