1

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'
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
karthi
  • 23
  • 1
  • 4

3 Answers3

2

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)

buddemat
  • 4,552
  • 14
  • 29
  • 49
0

Nothing wrong with your code. Possibly you are using a old version of ChromeDriver returning a the wrong shaped object.


Solution

Ensure that:


tl; dr

FIND_ELEMENT command return a dict object value

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Maybe you are trying to click on the element before it is ready to be clickable.

Try this

from selenium.webdriver.support.ui import WebDriverWait

elem = WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "._1DRQ8")))
elem.click()
Robson Sampaio
  • 307
  • 1
  • 5