0

Just started experimenting with selenium 4 using Python, I want to work with HtmlUnit too (to be deployed in a Debian OS without GUI). So I installed selenium-server-standalone-3.5.3.jar and I run it on http://127.0.0.1:4444/wd/hub.

But I got this error:

$ python selenium/myfile.py
Traceback (most recent call last):
  File "APPLICATION_PATH\selenium\myfile.py", line 81, in <module>
    Choices= TabList.find_elements(By.XPATH, '//li[@role="presentation"]')
AttributeError: 'dict' object has no attribute 'find_elements'

This is a piece of code with line 79 :

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from os.path import join, basename, dirname
import time
import datetime
import custom_wait_conditions as CWC
import os
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.chrome.service import Service as ChromeService


def mkdir_p(path):
  try:
     isExist = os.path.exists(path)
     if not isExist:
        os.makedirs(path, exist_ok=True)
  except (os.error, e):
    print(e.errno)

ROOT_DIR = os.path.abspath(os.curdir)

today = datetime.date.today();
today_date = str(today.strftime("%d/%m/%Y"))

download_folder = ROOT_DIR + r"\download"

# Create download files Folter if notexist
mkdir_p(download_folder);

chrome_options = webdriver.ChromeOptions()
chrome_options.set_capability("browserName", "htmlunit")
chrome_options.set_capability("version", "9.4.5.v20170502")
chrome_options.set_capability("platform", "WIN10")
chrome_options.set_capability("cssSelectorsEnabled", True)
chrome_options.set_capability("javascriptEnabled", True)
chrome_options.set_capability("w3c", True)

driver = webdriver.Remote(
  command_executor='http://127.0.0.1:4444/wd/hub',
  options=chrome_options
)

driver.get("THE_TARGET_LINK")

startDate = driver.find_element(By.ID, 'txtDateD')
endDate = driver.find_element(By.ID, 'txtDateF')
searchButton = driver.find_element(By.ID, 'btnRechercheAnnByDateTypeJur')
TabList = driver.find_element(By.XPATH, '//ul[@role="tablist" and 
@class="nav nav-pills TypeJuridiction"]')
Choices = TabList.find_elements(By.XPATH, '//li[@role="presentation"]')
resultTab = driver.find_element(By.ID, 'Res')
resultDetails = driver.find_element(By.ID, 'Res_Detail')

startDate.send_keys("14/03/2022")
endDate.send_keys(today_date)

I tried to disable some capabilities options, but I got the same error.

Malki Mohamed
  • 1,578
  • 2
  • 23
  • 40
  • 2
    always put code, data and full error message as text (not screenshot, not link) in question (not in comment). It will be more readable and easier to use in answer, and more people will see it - so more people can help you. – furas Mar 18 '22 at 17:31
  • 2
    Python can't load image with code to run it, mouse can't select text on image to copy it - so it is useless for us. Always put code as text, not image – furas Mar 18 '22 at 17:32
  • 2
    always put full error message (starting at word "Traceback") in question (not in comments) as text (not screenshot, not link to external portal). There are other useful information. – furas Mar 18 '22 at 17:33
  • 1
    I will update my code, sorry – Malki Mohamed Mar 18 '22 at 17:34
  • 1
    @AbhyudayVaish I agree with you – furas Mar 18 '22 at 17:36
  • it is strange because `TabList` should be `WebElement`, not `dict`. I tried to check it on some simple example and I can't reproduce this problem. It looks like you run differnt code which changes `TabList` – furas Mar 18 '22 at 17:39
  • 1
    There seems to be an issue with some versions of chromedriver used with selenium returning a dict instead of an element for `find_element(by.xpath)` – G. Anderson Mar 18 '22 at 17:41
  • Are you sure that this xpath is correct `//ul[@role="tablist" and @class="nav nav-pills TypeJuridiction"]` and returns a web element? try with `time.sleep(2)` before this command. – cruisepandey Mar 18 '22 at 17:50
  • 1
    @cruisepandey, yes because it works with chromedriver – Malki Mohamed Mar 18 '22 at 17:55
  • Ideally, you should be using explicit wait, cause driver.find_element look instantly in the HTML DOM, many times element/s have not been rendered properly causing these issues. – cruisepandey Mar 18 '22 at 17:56
  • 1
    @cruisepandey, `print(tablist)` displays `{'ELEMENT': '3'}` – Malki Mohamed Mar 18 '22 at 18:30
  • 1
    Seems like you are using selenium 4, this is a known issue to selenium community, you should update your chrome driver and browser to latest and this issue would get resolved by it's own. – cruisepandey Mar 18 '22 at 18:37

1 Answers1

-1

It seems that

TabList = driver.element... returns a dict. Dict does not have the function find_elements.

May be you want:

Choices= driver.find_elements(By.XPATH, './/li[@role="presentation"]')
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
May
  • 1
  • 2