RSelenium新手求助:无法定位并点击网页加载数据按钮以下载生成的CSV文件
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-primaryis invalid. If the button has an ID ofbtn-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"orclass="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

