属性错误:“字典”对象没有属性“find_elements”;

发布于 2025-01-15 03:52:28 字数 2524 浏览 3 评论 0原文

刚刚开始使用 Python 尝试 selenium 4,我也想使用 HtmlUnit(部署在没有 GUI 的 Debian 操作系统中)。 因此,我安装了 selenium-server-standalone-3.5.3.jar 并在 http://127.0.0.1:4444/wd/hub 上运行它。

但我得到了这个错误:

$ 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'

这是一段第 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)

我尝试禁用一些功能选项,但我得到了同样的错误。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟织青萝梦 2025-01-22 03:52:28

似乎

TabList = driver.element... 返回一个字典。
Dict 没有 find_elements 函数。

可能你想要:

Choices= driver.find_elements(By.XPATH, './/li[@role="presentation"]')

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"]')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文