如何单击按钮并使用机器人框架或硒下载文件,它不包含 link
how to click button and download a file using robot frame work or selenium, it not contains link
https://www.nasdaq.com/market-activity/stocks/screener - 我需要从这个站点下载 csv 文件,
如果在 selenium 或机器人框架中解决方案都很好。有参考代码指导一下就好了
我试过这个,它每天工作 2 次 -
我的要求不适合上面这个。
此外,我曾在 selenium 中工作,但它已被弃用并且效果不佳 -
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
PATH = "E:/chromedriver_win32/chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.nasdaq.com/market-activity/stocks/screener")
driver.implicitly_wait(10)
link = driver.find_element_by_class_name("nasdaq-screener__download")
link.click()
driver.close()
我也试过另一种方法,虽然行不通
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import time
options = Options()
options.add_argument("start-maximized")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
#driver.get("https://www.google.com")
page_url = "https://www.nasdaq.com/market-activity/stocks/screener"
driver.get(page_url)
time.sleep(5)
#driver.find_element(By.CLASS_NAME, "nasdaq-screener__form-button--download ns-download-1")
#kd = driver.find_element(By.ID, "onetrust-accept-btn-handler")
#kd.click()
title = driver.find_element(By.CLASS_NAME, "nasdaq-screener__form-button--download ns-download-2")
title.click()
driver.close()
#driver.close()
两个代码都给出相同的错误
错误
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".nasdaq-screener__form-button--download ns-download-2"}
(Session info: chrome=100.0.4896.127)
Stacktrace:
Backtrace:
Ordinal0 [0x006D7413+2389011]
Ordinal0 [0x00669F61+1941345]
Ordinal0 [0x0055C658+837208]
这应该有效:
driver.get("https://www.nasdaq.com/market-activity/stocks/screener")
try:
# Trying to click on 'Accept Cookies' if the footer banner appears
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='onetrust-accept-btn-handler']"))).click()
except: # else print the msg to console
print("No Cookie bar")
time.sleep(2) # a brief pause between two actions
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='nasdaq-screener__form-button--download ns-download-1' and contains(text(), 'Download CSV')]"))).click()
time.sleep(10) # wait for 10 seconds while the file is downloading
driver.close()
显式等待导入(时间除外)
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
https://www.nasdaq.com/market-activity/stocks/screener - 我需要从这个站点下载 csv 文件,
如果在 selenium 或机器人框架中解决方案都很好。有参考代码指导一下就好了
我试过这个,它每天工作 2 次 -
此外,我曾在 selenium 中工作,但它已被弃用并且效果不佳 -
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
PATH = "E:/chromedriver_win32/chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.nasdaq.com/market-activity/stocks/screener")
driver.implicitly_wait(10)
link = driver.find_element_by_class_name("nasdaq-screener__download")
link.click()
driver.close()
我也试过另一种方法,虽然行不通
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import time
options = Options()
options.add_argument("start-maximized")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
#driver.get("https://www.google.com")
page_url = "https://www.nasdaq.com/market-activity/stocks/screener"
driver.get(page_url)
time.sleep(5)
#driver.find_element(By.CLASS_NAME, "nasdaq-screener__form-button--download ns-download-1")
#kd = driver.find_element(By.ID, "onetrust-accept-btn-handler")
#kd.click()
title = driver.find_element(By.CLASS_NAME, "nasdaq-screener__form-button--download ns-download-2")
title.click()
driver.close()
#driver.close()
两个代码都给出相同的错误
错误
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".nasdaq-screener__form-button--download ns-download-2"}
(Session info: chrome=100.0.4896.127)
Stacktrace:
Backtrace:
Ordinal0 [0x006D7413+2389011]
Ordinal0 [0x00669F61+1941345]
Ordinal0 [0x0055C658+837208]
这应该有效:
driver.get("https://www.nasdaq.com/market-activity/stocks/screener")
try:
# Trying to click on 'Accept Cookies' if the footer banner appears
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='onetrust-accept-btn-handler']"))).click()
except: # else print the msg to console
print("No Cookie bar")
time.sleep(2) # a brief pause between two actions
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='nasdaq-screener__form-button--download ns-download-1' and contains(text(), 'Download CSV')]"))).click()
time.sleep(10) # wait for 10 seconds while the file is downloading
driver.close()
显式等待导入(时间除外)
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC