繁体   English   中英

Selenium Chromedriver:当禁用 w3c 时,'dict' 对象没有 find_element() 方法的属性 'click'

[英]Selenium Chromedriver : 'dict' object has no attribute 'click' for find_element() method when w3c is disabled

刚刚开始试验 selenium 和 chromedriver,我在特定配置中出现错误。

我总是得到:当我调用find_element()方法或find_element_by_*方法时, 'dict' object has no attribute 'click'

我发现该行:

options.add_experimental_option('w3c', False)

导致了这个问题,但不明白为什么以及如何解决这个问题。

问题:
- 这是正常行为吗?
- 关于如何解决这个问题的想法?

信息:我禁用了 w3c 以便能够检索状态代码的性能日志(网络)。

这是我制作的独立测试代码:

import os
import json

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

# Get Chrome Driver Fullpath
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
DRIVER_FULLPATH = os.path.join(PROJECT_ROOT, "chromedriver")


# Options
options = Options()

## Enable headless
options.headless = False
# Disable w3c to be able to retrieve performance logs
options.add_experimental_option('w3c', False)

# Specify custom chromedriver path
service = Service(DRIVER_FULLPATH)

# Create a DesiredCapabilities object
capabilities = DesiredCapabilities.CHROME.copy()

# Enable Network logging here too
capabilities['perfLoggingPrefs'] = ['enableNetwork']

# Instantiate the Driver
driver = webdriver.Chrome(options=options, service=service, desired_capabilities={'loggingPrefs': {'performance': 'INFO'}})

# The URL we want to load first (entry point)
url = 'https://www.python.org/'

# Load the page
driver.get(url)

# Get Logs
logs = driver.get_log('performance')

# Go through logs and find latest network response to get code status
for log_index in range(1,len(logs)+1):
    message = json.loads(logs[log_index]['message'])['message']
    if message and message['method'] == 'Network.responseReceived':
        status_code = message['params']['response']['status']
        # For demo purpose only print the status
        print('Page status : ' + str(status_code))
        break

# Select the radio button
elem = driver.find_element_by_class_name("donate-button")
# OR elem = driver.find_element(By.CLASS_NAME, "donate-button")

# This below will fail (raise error) since I disable w3c but how to fix that ?
elem.click()

input('Press ENTER to close the automated browser')

# Exit
driver.quit()

我的配置:

  • Mac OS 大苏尔 11.5
  • 蟒蛇 3.9.6
  • 硒 4.0.0.b4
  • 铬 92.0.4515.131
  • ChromeDriver 92.0.4515.43

我遇到了完全相同的情况,但是将 selenium 版本降级到 3.141.0 解决了我的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM