点击网页按钮下载

问题描述 投票:0回答:1
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 ="油漆"?

现场有“保存图像”按钮。一旦我们点击它,图表就会自动下载。我正在尝试模拟 click () 但它不起作用。我可能选择了错误的 id 或者可能是我使用了错误的元素交互?

有人可以帮助我吗?

python selenium-webdriver buttonclick
1个回答
0
投票

您定位的整个图表都在 iframe 中。您需要先切换到该框架,然后才能访问其中的元素。

# 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()
© www.soinside.com 2019 - 2024. All rights reserved.