selenium.common.exceptions.WebDriverException:消息:尝试使用Selenium和Python单击元素时,无法将数据转换为对象 [英] selenium.common.exceptions.WebDriverException: Message: Failed to convert data to an object while trying to click an element with Selenium and Python
问题描述
在登录按钮后尝试在页面上单击按钮的是以下HTML
im trying to click a button on a page after logging in the button is the following HTML
<div id="carrierDashboardControls">
<button class="yms-button-primary" ng-click="refresh()">
<t>Refresh</t>
</button>
<button class="yms-button-primary-alt ng-isolate-scope" ng-csv="fetchData()" lazy-load="true"
csv-header="getCsvHeader" filename="carrier-dashboard.csv" field-separator=",">CSV
</button>
</div>
在此有2个按钮,我想单击一个与班级有关的按钮"yms-button-primary-alt ng-isolate-scope"但是我得到以下错误
there are 2 buttons in this and i want to click the one with class "yms-button-primary-alt ng-isolate-scope" however i get the follwing error
单击此按钮将下载CSV文件,但现在我收到错误消息"selenium.common.exceptions.WebDriverException:消息:无法将数据转换为对象"
this button will download a CSV file when clicked but right now i get the error "selenium.common.exceptions.WebDriverException: Message: Failed to convert data to an object"
当前正在使用以下代码,请注意,由于业务性质,实际的网址无法共享(由于登录后发生重定向,我两次导航到该网址)
im currently using the below code, note the actual url's cannot be shared due to the nature of the business (i navigate to the url twice due to a redirection after login)
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import re
import pandas as pd
import os
url = "THE URL"
username = 'USERNAME'
password = 'PASSWORD'
driver = webdriver.Firefox(executable_path=r'MYPATH/geckodriver.exe')
driver.implicitly_wait(100)
driver.get(url)
user_field = driver.find_element_by_id("ap_email")
pass_field = driver.find_element_by_id("ap_password")
sign_in = driver.find_element_by_id("signInSubmit")
user_field.send_keys(username)
pass_field.send_keys(password)
sign_in.click()
driver.implicitly_wait(100)
driver.get(url)
CSV_BUTTON = driver.find_element_by_class_name("yms-button-primary-alt ng-isolate-scope")
CSV_BUTTON.click()
作为补充,我想处理此后下载的文件,因为如果可能的话,我希望使用当前日期和时间自动重命名该文件?
as an added note i would like to manipulate the file that is downloaded afterwards as i would like to have it auto renamed with current date and time if this is possible ?
下面的完整堆栈
Traceback (most recent call last):
File "C:/Users/USER/PycharmProjects/YMS scrape/venv/YMS Sel#.py", line 26, in <module>
CSV_BUTTON = driver.find_element_by_class_name("yms-button-primary-alt ng-isolate-scope")
File "C:\Users\USER\Anaconda3\envs\YMS scrape\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 564, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "C:\Users\USER\Anaconda3\envs\YMS scrape\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\Users\USER\Anaconda3\envs\YMS scrape\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\USER\Anaconda3\envs\YMS scrape\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Failed to convert data to an object
推荐答案
所需元素是动态元素,可通过延迟加载,因此要对元素进行 click()
,就必须诱导 WebDriverWait 为 element_to_be_clickable()
,您可以使用以下任一
The desired element is an dynamic element which becomes visible through lazy-loading, so to click()
on the element you have to induce WebDriverWait for the element_to_be_clickable()
and you can use either of the following Locator Strategies:
-
使用
CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.yms-button-primary-alt.ng-isolate-scope[csv-header='getCsvHeader'][ng-csv^='fetchData']"))).click()
使用 XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='yms-button-primary-alt ng-isolate-scope' and @csv-header='getCsvHeader'][contains(., 'CSV')]"))).click()
注意:您必须添加以下导入:
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
这篇关于selenium.common.exceptions.WebDriverException:消息:尝试使用Selenium和Python单击元素时,无法将数据转换为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!