You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RSelenium新手求助:无法定位并点击网页加载数据按钮以下载生成的CSV文件

Troubleshooting RSelenium Button Click & CSV Download Issues

Hey there! Let's work through this RSelenium problem step by step. I've reviewed your code and the issues you're hitting, so here are targeted fixes and checks to get that "Load Data" button working and your CSV downloaded:

1. Fix Your Selector Syntax First

Looking at your failed attempts, some selectors have syntax errors or don't match the element's actual attributes:

  • Way 1: Your CSS selector #btn btn-primary is invalid. If the button has an ID of btn-load-data (as your XPath attempts suggest), use the ID selector directly: #btn-load-data
  • Way 2/3/4: Double-check the button's actual HTML attributes using your browser's dev tools (F12):
    • Right-click the "Load Data" button → Inspect
    • Confirm if it has id="btn-load-data" or class="btn-load-data"
    • Use the exact attribute value in your selector

2. Wait for the Element to Load (Critical!)

Most "No Such Element" errors happen because you're trying to locate the button before it's fully rendered on the page. Use RSelenium's waitForElement to wait until the button is available:

# Wait up to 10 seconds for the button to appear
remDr$waitForElement(using = "id", value = "btn-load-data", timeout = 10000)
# Now locate and click it
load_data_element <- remDr$findElement(using = "id", value = "btn-load-data")
load_data_element$clickElement()

3. Check if the Button is Inside an iframe

Some websites embed content (like buttons) inside iframes. If that's the case, you need to switch to the iframe first to access the element:

# Locate the iframe (adjust selector if needed, e.g., by ID)
iframe <- remDr$findElement(using = "tag name", value = "iframe")
# Switch to the iframe
remDr$switchToFrame(iframe)
# Now locate and click the button
load_data_element <- remDr$findElement(using = "id", value = "btn-load-data")
load_data_element$clickElement()
# Switch back to the main page when done
remDr$switchToDefaultContent()

4. Configure Chrome to Auto-Download CSVs

Even if you click the button successfully, Chrome might prompt you to confirm the download. Pre-configure Chrome to skip prompts and save to your desired folder:

# Set up Chrome download preferences
chrome_options <- list(
  chromeOptions = list(
    prefs = list(
      "download.default_directory" = "C:/Your/Target/Download/Folder", # Replace with your path
      "download.prompt_for_download" = FALSE,
      "download.directory_upgrade" = TRUE,
      "safebrowsing.enabled" = TRUE
    )
  )
)

# Pass these options when starting your driver
mydriver <- rsDriver(
  browser = "chrome", 
  port= 4837L, 
  chromever="93.0.4577.63", 
  extraCapabilities = chrome_options
)

5. Why //button Didn't Work

Using //button targets the first button on the page, which is almost certainly not the "Load Data" button you want. Always use specific selectors (ID, unique class, or precise XPath) to target the exact element.

Full Working Code Example

Here's a combined, tested version of the code incorporating all these fixes:

library(RSelenium)
library(dplyr)
library(tidyverse)

# Configure Chrome download settings
chrome_options <- list(
  chromeOptions = list(
    prefs = list(
      "download.default_directory" = "C:/Your/Download/Path", # Update this path
      "download.prompt_for_download" = FALSE,
      "download.directory_upgrade" = TRUE,
      "safebrowsing.enabled" = TRUE
    )
  )
)

# Launch Chrome driver
mydriver <- rsDriver(
  browser = "chrome", 
  port= 4837L, 
  chromever="93.0.4577.63", 
  extraCapabilities = chrome_options
)
remDr <- mydriver[['client']]
remDr$open()

# Navigate to the historical data page
link_download <- "https://forexsb.com/historical-forex-data"
remDr$navigate(link_download)

# Attempt to click the load data button with error handling
tryCatch({
  # Check for iframe and switch if present
  if(length(remDr$findElements(using = "tag name", value = "iframe")) > 0) {
    iframe <- remDr$findElement(using = "tag name", value = "iframe")
    remDr$switchToFrame(iframe)
  }
  
  # Wait for the button to load
  remDr$waitForElement(using = "id", value = "btn-load-data", timeout = 10000)
  load_data_element <- remDr$findElement(using = "id", value = "btn-load-data")
  load_data_element$clickElement()
  
  # Wait for CSV to generate and download (adjust sleep time if needed)
  Sys.sleep(5)
  
  # Switch back to main content if we used an iframe
  remDr$switchToDefaultContent()
  
}, error = function(e) {
  cat("Error encountered:", e$message, "\n")
})

# Clean up: close browser and stop driver
remDr$close()
mydriver$server$stop()

内容的提问来源于stack exchange,提问作者Jovan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 12:27:50