Rselenium - 如何禁用 Firefox 配置文件中的图像

问题描述 投票:0回答:2

Rselenium 中使用 Firefox 时如何禁用图片下载?我想看看这样做是否会使抓取脚本更快。

我已阅读 Reselnium 软件包手册,包括有关 getFirefoxProfile 和 makeFirefoxProfile 的部分。

我发现这个链接显示了如何处理 chromedriver

我可以禁用在 Windows 10 中手动打开的 Firefox 实例的图像,但 Rselenium 似乎不使用相同的配置文件。

r windows firefox rselenium
2个回答
2
投票

以前您需要设置适当的首选项(在本例中

permissions.default.image
)但是现在 Firefox 重置此值时出现问题,请参阅:

https://github.com/seleniumhq/selenium/issues/2171

给出了解决方法:

https://github.com/gempesaw/Selenium-Remote-Driver/issues/248

RSelenium
中实现这一点:

library(RSelenium)
fprof <- makeFirefoxProfile(list(permissions.default.image = 2L,
                                 browser.migration.version = 9999L))
rD <- rsDriver(browser = "firefox", extraCapabilities = fprof)
remDr <- rD$client
remDr$navigate("http://www.google.com/ncr")
remDr$screenshot(display = TRUE)

# clean up
rm(rD)
gc()

0
投票

问题:我尝试在 Firefox 中使用 R 和 RSelenium 禁用图像来执行网页抓取任务。

初步尝试:我首先尝试使用makeFirefoxProfile,但没有产生预期的结果。

解决方案:我最终通过直接设置浏览器参数来解决这个问题,类似于抓取时与元素交互的方式。

# Initiate the RSelenium driver for Firefox
client_server <- RSelenium::rsDriver(browser = "firefox", port = 4444L, chromever = NULL)
driver <- client_server[["client"]]

# Navigate to Firefox's about:config to tweak settings
driver$navigate("about:config")
Sys.sleep(3)

# Accept the warning to access settings
driver$findElement(using = "css selector", value = "#warningButton")$clickElement()

# Search for the image permission setting
webElem <- driver$findElement(using = "xpath", value = "//*[@id='about-config-search']")
webElem$sendKeysToElement(list("permissions.default.image"))
Sys.sleep(4)

# Modify the image load settings (2 means images won't get loaded)
driver$findElement(using = "xpath", value = "/html/body/table/tr[1]/td[2]/button")$clickElement()
webElem <- driver$findElement(using = "xpath", value = "/html/body/table/tr[1]/td[1]/form/input")
webElem$sendKeysToElement(list("2"))
Sys.sleep(3)

# Confirm changes
driver$findElement(using = "css selector", value = ".primary")$clickElement()

© www.soinside.com 2019 - 2024. All rights reserved.