[英]Not able to click on the button using Selenium
<button class="css-obkt16-button" type="button"><span class="css-1mhnkuh">Download CSV</span></button>
我正在尝试单击突出显示的按钮“下载 CSV ” 具有上述 HTML 代码并将 csv 文件保存在某个特定位置,但我无法这样做。 该文件正在下载文件夹中下载。
我的 python 代码:
def scrape_data():
DRIVER_PATH = r"C:\chrome\chromedriver.exe"
driver = webdriver.Chrome(DRIVER_PATH)
driver.get('Link to the dashboard')
time.sleep(20)
buttons = driver.find_element(By.XPATH,"//button/span[text()='Download CSV']")
time.sleep(5)
driver.execute_script("arguments[0].click();", buttons)
driver.quit()
所以请建议一种通过按钮文本搜索的方法)并将文件保存到特定位置??
要将文件下载到特定位置,您可以像打击一样尝试。
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_experimental_option("prefs", {
"download.default_directory": r"C:\Data_Files\output_files"
})
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
time.sleep(20)
这样的硬编码睡眠。 应该改用WebDriverWait
expected_conditions
。def scrape_data():
DRIVER_PATH = r"C:\chrome\chromedriver.exe"
driver = webdriver.Chrome(DRIVER_PATH)
wait = WebDriverWait(driver, 30)
driver.get('Link to the dashboard')
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(.,'Download CSV')]"))).click()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.