AttributeError: 'dict' object has no attribute 'click' error with find_element_by_css_selector() and click() using Selenium and ChromeDriver

I want to use the click command on my test page. But I'm getting the below error. I've written it in python

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
import pandas as pd

url="https://tv.com/home"
options = webdriver.ChromeOptions()
options.binary_location ="C:\Program Files\Google\Chrome\Application\chrome.exe"
driver = webdriver.Chrome("chromedriver.exe",chrome_options=options)

driver.get(url
driver.maximize_window()
time.sleep(20)
arr=[]
driver.find_element_by_css_selector("._1DRQ8").click()
x=driver.find_element_by_css_selector("._1sZ9q").find_elements_by_tag_name("a")

Getting Error :

driver.find_element_by_css_selector("._1DRQ8").click()

AttributeError: 'dict' object has no attribute 'click'

Solution 1:

Latest changes on selenium and chromedriver may result in errors like:

[AttributeError: 'dict' object has no attribute ... error .... using Selenium and ChromeDriver]

To fix AttributeError: 'dict' object has no attribute:

  1. Implement a step to check if this dict in the result.
  2. Create WebElement from the value of the dictionary.

Here is an example of the code:

def get_web_element_from_dict_if_it_is(driver: WebDriver, element_to_check_for_dict):
if type(element_to_check_for_dict) is dict:
    first_element_value = list(element_to_check_for_dict.values())[0]
    element_to_check_for_dict = driver.create_web_element(element_id=first_element_value)
return element_to_check_for_dict

PS:

  • If your current result is dict - it will create a new element from the value in the dictionary.
  • If it is not a dictionary you will use this element as usual.

This approach can be related to any selenium method for operating with selenium element (find the element, execute script) and any language which uses selenium(python, java, etc)