为什么使用 RSelenium 的 Firefox 无头模式下的 DOM 结构不同?

问题描述

我正在尝试单击按钮以在页面上从 https://bostad.blocket.se/p2/sv/find-home/ 列出更多属性。如果我在正常模式下将 RSelenium 与 Firefox 一起使用,一切正常,但如果我使用无头 Firefox,DOM 结构会有所不同(我想在 VPS 上使用此脚本,因此常规 Firefox 会出现问题)。

我的代码

library(RSelenium)

url <- "https://bostad.blocket.se/p2/sv/find-home/"

driver  <- rsDriver(browser = "firefox",check = FALSE,verbose = FALSE,port = 4567L,javascript = TRUE,nativeEvents = TRUE,extraCapabilities = list("moz:firefoxOptions" = 
        list(args = list('--headless'))))

remDr <- driver$client
remDr$navigate(url)

# the page in headless Firefox only has Feedback buttons,nothing else ()
cookie_button <- remDr$findElement(using = "css",value = "button[class='sc-hKgILt bFzSgw blocket-cookie-consent___StyledButton-fscird-8 epUktU']")
cookie_button$clickElement()

button <- remDr$findElement(using = "css",value = "button[class='sc-hKgILt hiKJbb pagination__StyledButton-sc-17c2n48-2 kFYVIO']")
button$clickElement()

我尝试增加超时时间 (remDr$setTimeout(milliseconds = 100000)),但是没有用。 另外,我注意到node的style属性不同,所以尝试注入简单的JavaScript代码(我没有这种语言的命令,所以代码可能会出错):

script <- 'document.getElementById("body").style.overflow="visible";'

remDr$executeScript(script,args = list())

我尝试添加到网页的 JavaScript 代码也不起作用,那么我应该如何解决在常规和无头 Firefox 模式下具有不同 DOM 结构的问题?当浏览器处于无头模式时,是否有任何防止网络抓取的保护?如果没有明显的解决方案,我将有兴趣了解如何将 Linux 包 xvfb 与 RSelenium 结合使用(我知道 Python 可以使用这种选项)。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)