提问人:jojo okolie 提问时间:9/18/2023 最后编辑:jojo okolie 更新时间:9/19/2023 访问量:38
Selenium 按钮单击在页面 URL 上未按预期工作
Selenium button click is not working as expected on the page URL
问:
我正在尝试通过单击下载按钮从 Web 链接下载 JSON 文件,但即使我使用正确的 ID 或选择器,下载按钮也没有响应。
下面是我的代码和我尝试从中提取数据的网站。
问题出在这条线上.我需要帮助来修复它。谢谢 driver.find_element(By.ID, "exportResultsButton").click()
暂无回应。😭
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
# Initialize the Chrome web driver
driver = webdriver.Chrome()
# Navigate to the webpage
url = 'https://ntsb.gov/Pages/monthly.aspx'
driver.get(url)
driver.maximize_window()
# Find all elements matching your XPath selector
link_elements = driver.find_elements(By.XPATH, '//*[@id="NTSBContentArea"]//span//a')
links_list = set()
# Extract and print the links
for link_element in link_elements:
link = link_element.get_attribute('href')
links_list.add(link)
# Loop through the links and download JSON data
for link in links_list:
driver.get(link)
try:
time.sleep(15)
# Click the "Download Data (JSON)" button
driver.find_element(By.ID, "exportResultsButton").click()
# Retrieve and print or save the JSON data
json_data = driver.page_source
# Process or save the JSON data as needed
print(json_data)
except Exception as e:
print(f"Error downloading data from {link}: {str(e)}")
# Close the browser
driver.quit()
答: 暂无答案
评论