问题
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>
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
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 ?
FULL STACKTRACE BELOW
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
回答1:
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:
Using
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()
Using
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
来源:https://stackoverflow.com/questions/58878733/selenium-common-exceptions-webdriverexception-message-failed-to-convert-data-t