想要使用Selenium和python下载数据

时间:2018-10-30 12:52:22

标签: python-3.x selenium selenium-webdriver selenium-chromedriver webdriverwait

嗨,我想从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。

谢谢。

2 个答案:

答案 0 :(得分:1)

一旦您访问 url https://www.aemo.com.au/Electricity/National-Electricity-Market-NEM/Data-dashboard#price-demand以下载所需的数据,就需要:

  • 诱导 WebDriverWait 以使所需的框架可用并切换到它。
  • WebDriverWait 提供所需的元素,其文本为 SA ,以便单击。
  • 再次诱导 WebDriverWait 以使所需元素可单击。
  • 您可以使用以下解决方案:
  • 代码块:

    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()
    
  • 浏览器快照:

aemo

答案 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()