gpt4 book ai didi

python - 按下按钮然后下载 csv 文件

转载 作者:行者123 更新时间:2023-12-01 09:33:05 27 4
gpt4 key购买 nike

我正在编写一个代码,我需要登录一个网站,登录后我按某个按钮,然后下载 csv 文件。当我按下另一个按钮时,该代码对我有用。但是,我需要按下某个按钮,其中包含 csv 文件中我需要的所有数据,并且不会单击或给我任何错误。无论如何,这是我的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
import time
import re
chromedriver = "/webdrivers/chromedriver"
driver = webdriver.Chrome(chromedriver)
driver.maximize_window()
driver.get('https://user.sensogram.com/signin') #driver.get(url)-- We get
url by using driver which we initialy load.
print ("Opened sensogram")
time.sleep(5) #Just wait for sometime.
email = driver.find_element_by_xpath("//input[@name='usernameSignIn']")#Find
email textaera.
email.send_keys('*****') #Send email to this text area.
password = driver.find_element_by_xpath("//input[@name='passwordSignIn']")
#Find password textarea.
password.send_keys('*****') #send password to the password field.
button_to_login = driver.find_element_by_xpath("//button[@ng-
click='submitted=true']") #Find login button.
button_to_login.click() #Click on login button.
time.sleep(5)
Custom_Graph_Button = driver.find_element_by_xpath("//*[@class='link_title'
and contains (text(),'Custom Graph')]").click()
time.sleep(5)
while True:
if EC.element_to_be_clickable:
try:
csv_file_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//*[name()='tspan' and .='Download CSV']"))).click() #find the download button by Xpath of the HTML then click on Download CSV button
print("\n Button is Clicked")
time.sleep(5) #download every 1 minute
#print(csv_file_button)
except TimeoutException:
pass
time.sleep(10)
continue
else:
pass

包含我需要的所有数据的按钮是自定义图形按钮,我可以单击它,但我现在坚持的是按 CSV 文件按钮。有人可以帮忙吗!!!我是 python 新手

最佳答案

WebDriverWait 更改为 find_elements_by_class_name 以查找包含 class“highcharts-button”的所有按钮。

import time
from selenium import webdriver

path = "/webdrivers/chromedriver"

driver = webdriver.Chrome(executable_path=path)
driver.maximize_window()
driver.get('https://user.sensogram.com/signin') #driver.get(url)-- We get url by using driver which we initialy load.
print ("Opened sensogram")
time.sleep(5) #Just wait for sometime.
email = driver.find_element_by_xpath("//input[@name='usernameSignIn']")#Find email textaera.
email.send_keys('username') #Send email to this text area.
password = driver.find_element_by_xpath("//input[@name='passwordSignIn']")
#Find password textarea.
password.send_keys('password') #send password to the password field.
button_to_login = driver.find_element_by_xpath("//button[@ng-click='submitted=true']") #Find login button.
button_to_login.click() #Click on login button.
time.sleep(2)

driver.find_element_by_xpath("//*[@class='link_title' and contains (text(),'Custom Graph')]").click()

# get all buttons and find one that has 'Download CSV' text

btns = driver.find_elements_by_class_name("highcharts-button")
for btn in btns:
if btn.is_displayed() and btn.text == 'Download CSV':
btn.click()

time.sleep(4)
driver.close()

关于python - 按下按钮然后下载 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49783884/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com