Download by clicking button on webpage

63 views Asked by At
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys


options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
options.add_experimental_option('excludeSwitches', \['enable-logging'\])

s = Service('C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)

driver.get("https://chartink.com/stocks/nifty.html")

driver.maximize_window()
time.sleep(10)

element1 = driver.find_element(By.ID,'paint')
driver.execute_script("arguments\[0\].click();", element1);[tag:tag-name]

--- ID ="paint"?

On site there is button "SAVE IMAGE". Once we click it charts are downloaded automatically. I am trying to simulate click () but it is not working. I might be selecting wrong id or may be I using wrong interaction with element?

Can someone help me?

2

There are 2 answers

2
Chadee Fouad On BEST ANSWER

Have you considered using Selenium Helium library? https://selenium-python-helium.readthedocs.io/en/latest/api.html

It makes it super easy to automate the browser without having to deal with the technical complexities. It is very user friendly and takes care of that behind the scenes. You just have to tell it which button to click in 'plain English' Examples:

click("SAVE IMAGE")


click("Paint")
click("Sign in")
click(Button("OK"))
doubleclick(Image("Directories"))

More details can be found in the documentation. Hope that helps!

1
Shine J On

The entire Chart you're targetting, is inside an iframe. You need to switch to that frame before you can access the elements inside it.

# switching to iframe that contains the Chart Image
driver.switch_to.frame('ChartImage')
time.sleep(1)
# clicking the button
element1 = driver.find_element(By.ID,'saverbutton')
driver.execute_script("arguments[0].click();", element1)

# switching back to default frame
driver.switch_to.default_content()