r语言 - Selenium - 单击搜索按钮给出错误"element is not attached to the page document"



我想输入邮政编码并点击搜索使用Rselenium的网站

library(RSelenium)  
library(netstat)

zip <- "601"
rs_driver_object <- rsDriver(browser = 'chrome',
chromever = "101.0.4951.41",
verbose = F,
port = free_port())
remDr <- rs_driver_object$client
remDr$open()
remDr$maxWindowSize()
# go to website
remDr$navigate("https://pizza.dominos.com/")

# locate the search box
address_element <- remDr$findElement(using = 'id', value = 'geo-search')
# locate the search button
button_element <- remDr$findElement(using = 'class', value = "btn-search")

# send the zip code
address_element$sendKeysToElement(list(zip))

# click on search
button_element$clickElement()

然而,当我执行点击的最后一步时,它显示了错误:

Selenium message:stale element reference: element is not attached to the page document
(Session info: chrome=101.0.4951.67)
Error:     Summary: StaleElementReference
Detail: An element command failed because the referenced element is no longer attached to the DOM.
class: org.openqa.selenium.StaleElementReferenceException
Further Details: run errorDetails method

我能够使用以下代码实现这一点。错误stale element reference: element is not attached to the page document通常出现在后端HTML数据被更改并且您使用的xpath过时时。

# go to website
remdr$navigate("https://pizza.dominos.com/")
# locate the search box and enter the zip
zip <- "601"
address_element <- remdr$findElement(using = 'id', value = 'geo-search')$sendKeysToElement(list(zip))
# click on search
button_element <- remdr$findElement(using = 'xpath', value = "(//button[@class='btn-search'])[2]")$clickElement()

相关内容

最新更新