gpt4 book ai didi

r - 使用 Rselenium 在下拉框中选择选项

转载 作者:行者123 更新时间:2023-12-05 04:35:40 25 4
gpt4 key购买 nike

我正在尝试使用 RSelenium 从 https://analytics.phe.gov.uk/apps/covid-19-indirect-effects/# 的心理健康和福祉选项卡的下拉框中选择主题“孤独” .我可以让 Rselenium 进入心理健康选项卡,但我没有运气选择“孤独”主题。我将不胜感激任何指导,因为我已经审阅了 Stack Overflow 中的许多帖子(您可以对我多次失败的尝试轻笑)但仍然不高兴。

如有任何指点,我将不胜感激!

library(tidyverse)
library(rvest)
library(RSelenium)


# Start Selenium server and browser

driver <- rsDriver(
browser = "chrome",
chromever = "97.0.4692.71"
)

remDr <- driver$client

# Navigate to wich tool
remDr$navigate("https://analytics.phe.gov.uk/apps/covid-19-indirect-effects/")

#changing the page to the mental health and well being tab
##avoided using href as the tab value keeps changing

webElem <- remDr$findElement(using = 'xpath', value = '//*[@id="page"]/nav/div/ul/li[8]/a')

# Highlight to check that was correctly selected - don't need but putting it in as a check

webElem$highlightElement()

# click the search link
webElem$clickElement()


# click the link
webElem$clickElement()

#multiple failed attempts of trying to selecting the theme = Loneliness - can't get to work :-(
##attempt 1
webElem <- remDr$findElement(using = 'xpath', value = "//*[@data-shinyjs-resettable-id='themeOPN']")
webElem$highlightElement()
webElem$clickElement()

webElem1 <- remDr$findElement(using = 'xpath', value = "//*[@data-selectable data-value='Loneliness']")
webElem1$highlightElement()
webElem1$clickElement()

##
##attempt 2
webElem <- remDr$findElement(using = 'xpath', value = "//*[@data-shinyjs-resettable-id='themeOPN']")
webElem$highlightElement()
webElem$clickElement()

webElem2 <- remDr$findElement(using = 'xpath', value = "/html/body/div[1]/div[2]/div/div[8]/div[3]/div[1]/form/div[1]/div/div/div[2]/div/div[2]")
webElem2$highlightElement()
webElem2$clickElement()

##
##attempt 3

webElem <- remDr$findElement(using = 'xpath', value = "//*[@data-shinyjs-resettable-id='themeOPN']")
webElem$highlightElement()
webElem$clickElement()

webElem3 <- remDr$findElement(using = 'xpath', value = "//*[text()='Loneliness']")
webElem3$highlightElement()
webElem3$clickElement()

##
##attempt 4

webElem <- remDr$findElement(using = 'xpath', value = "//*[@data-shinyjs-resettable-id='themeOPN']")
webElem$highlightElement()
webElem$clickElement()

webElem4 <- remDr$findElement(using = 'xpath', "/html/body/div[1]/div[2]/div/div[8]/div[3]/div[1]/form/div[1]/div/div/div[2]/div/[@value = 'Loneliness']")
webElem4$highlightElement()
webElem4$clickElement()


##
##attempt 5

webElem <- remDr$findElement(using = 'xpath', value = "//*[@data-shinyjs-resettable-id='themeOPN']")
webElem[[2]]$clickElement()

最佳答案

看起来下拉菜单正在使用 selectize.js .像下面这样的东西似乎有效:

library(tidyverse)
library(rvest)
library(RSelenium)


# Start Selenium server and browser

driver <- rsDriver(
browser = "chrome",
chromever = "98.0.4758.80"
)

remDr <- driver$client

# Navigate to wich tool
remDr$navigate("https://analytics.phe.gov.uk/apps/covid-19-indirect-effects/")
select_tab <- remDr$findElement(using = "xpath", value = '//*[@id="indicatortable"]/div/div[1]/div[2]/div[6]/div/div[6]/div/a')

# Highlight to check that was correctly selected - don't need but putting it in as a check

select_tab$highlightElement()
select_tab$clickElement()

remDr$executeScript("
var s = $('#themeOPN').selectize()[0];
s.selectize.setValue('Loneliness');
")

关于r - 使用 Rselenium 在下拉框中选择选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70989818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com