嗨,我想从Link下载数据 我要从“ SA”区域下载数据的位置。我尝试了以下代码,在选择“ SA”标签后,我想单击“ 30分钟”标签上方的下载箭头。
chromedriver = "/usr/lib/chromium-browser/chromedriver"
driver = webdriver.Chrome(chromedriver)
driver.get('https://www.aemo.com.au/Electricity/National-Electricity-Market-NEM/Data-dashboard#price-demand')
time.sleep(5)
driver.find_element_by_xpath("//*[@class='au-target btn btn-default btn-lg active' and text()='SA']").click()
button = driver.find_element_by_xpath("//button[@class='btn btn-default au-target' and click.trigger='clickDownload($event)']")
button.click()
但是它会引发错误
消息:否这样的元素:无法找到元素: {“ method”:“ xpath”,“ selector”:“ // * [@ class ='au-target btn btn-default btn-lg active'和text()='SA']“}
您可以通过访问链接获取xpath。
谢谢。
答案 0 :(得分:1)
一旦您访问 url https://www.aemo.com.au/Electricity/National-Electricity-Market-NEM/Data-dashboard#price-demand
以下载所需的数据,就需要:
代码块:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get('https://www.aemo.com.au/Electricity/National-Electricity-Market-NEM/Data-dashboard#price-demand')
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@class='iframe-dashboard-MTO' and@src='/aemo/apps/visualisations/elec-nem-priceanddemand.html']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='au-target btn btn-default btn-lg' and contains(.,'SA')]"))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='btn-group']//following::button[1]/i[@class='icon-download']"))).click()
浏览器快照:
答案 1 :(得分:0)
两个按钮都位于iframe内,因此您应该在单击按钮之前切换到该框架:
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver.switch_to.frame(driver.find_element_by_class_name('iframe-dashboard-MTO'))
wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='SA']"))).click()
wait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, 'icon-download'))).click()