Ben*_*het 4 python selenium python-3.x selenium-chromedriver selenium-webdriver
刚刚开始尝试 selenium 和 chromedriver,我在特定配置中出现错误。当我调用一个或多个方法时
,我总是得到 : 。'dict' object has no attribute 'click'find_element()find_element_by_*
我发现该行:
options.add_experimental_option('w3c', False)
Run Code Online (Sandbox Code Playgroud)
引起了这个问题,但不明白为什么以及如何解决这个问题。
问题:
- 这是正常行为吗?
- 关于如何解决这个问题的想法?
信息:我禁用了 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()
Run Code Online (Sandbox Code Playgroud)
我的配置:
| 归档时间: |
|
| 查看次数: |
1824 次 |
| 最近记录: |