‘WebDriver’ no attribute ‘find_element_by_css_selector’ – Code Example

Total
0
Shares

Python Selenium throws error AttributeError: ‘WebDriver’ object has no attribute ‘find_element_by_css_selector’ when you try to use deprecated function find_element_by_css_selector. It is replaced by find_element(By.CSS_SELECTOR, element).

Code Example –

This code will throw error –

from selenium import webdriver
import time
import pandas as pd

url = 'https://akashmittal.com/'

wd = webdriver.Chrome(executable_path=r'/Users/akash/chromedriver')
wd.get(url)

no_of_icons = int(wd.driver.find_element_by_css_selector('p>span').get_attribute('innerText'))

If you run this code, the output will be –

Traceback (most recent call last):
  File "/Users/akash/jobscrape.py", line 10, in <module>
    no_of_icons = int(wd.find_element_by_css_selector('p>span').get_attribute('innerText'))
AttributeError: 'WebDriver' object has no attribute 'find_element_by_css_selector'

Solution –

To resolve this error use find_element(By.CSS_SELECTOR, 'p>span')

from selenium import webdriver
import time
import pandas as pd

url = 'https://akashmittal.com/'

wd = webdriver.Chrome(executable_path=r'/Users/akash/chromedriver')
wd.get(url)

no_of_icons = int(wd.find_element(By.CSS_SELECTOR, 'p>span'))

According to Selenium docs, all functions find_element_by_* are replace by these –

find_element(By.ID, "id")
find_element(By.NAME, "name")
find_element(By.XPATH, "xpath")
find_element(By.LINK_TEXT, "link text")
find_element(By.PARTIAL_LINK_TEXT, "partial link text")
find_element(By.TAG_NAME, "tag name")
find_element(By.CLASS_NAME, "class name")
find_element(By.CSS_SELECTOR, "css selector")