我正在编写一个代码,我需要登录一个网站,登录后我按下某个按钮然后下载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的新手
答案 0 :(得分:1)
将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()