Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ryansmccoy/152833d2e476a49649cf693f8155bbe3 to your computer and use it in GitHub Desktop.
Save ryansmccoy/152833d2e476a49649cf693f8155bbe3 to your computer and use it in GitHub Desktop.
# r'https://pypi.python.org/pypi/selenium
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import os, time, errno, sys
import lxml
def main():
if len(sys.argv) < 2:
sys.exit(0)
else:
url = r'https://www.esios.ree.es/en/analysis/600?vis=1&start_date=04-12-2016T00%3A00&end_date=04-12-2016T23%3A00&compare_start_date=03-12-2016T00%3A00&groupby=hour'
url = sys.argv[1]
# use chrome webdriver
#w = webdriver.Chrome()
# use default phantom
# w = webdriver.PhantomJS()
# USE phantom set custom user-agent
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = ('Mozilla/5.0 (Linux; Android 6.0.1; Nexus 6P Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.83 Mobile Safari/537.36')
w = webdriver.PhantomJS(desired_capabilities=dcap)
# get url
w.get(url)
# display w.page_source using BS
soup = BeautifulSoup(w.page_source, "lxml")
print(soup.prettify)
time.sleep(5)
# click the export button using xpath
xpath_export = r'//*[@id="dashboardView"]/form/div[7]/div'
w.find_element_by_xpath(xpath_export).click()
# click the csv button using xpath
xpath_export_csv = '//*[@id="dashboardView"]/form/div[7]/div/ul/li[1]/a'
w.find_element_by_xpath(xpath_export_csv).click()
if __name__ == '__main__':
# python selium_click_export_csv.py 'url of webpage'
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment