栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在R中访问Selenium API

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在R中访问Selenium API

可以使用JsonWireProtocol访问Selenium 。

首先,通过以下命令从命令行启动Selenium服务器:

java -jar selenium-server-standalone-2.25.0.jar

可以按以下方式打开新的Firefox浏览器:

library(RCurl)library(RJSONIO)library(XML)baseURL<-"http://localhost:4444/wd/hub/"server<-list(desiredCapabilities=list(browserName='firefox',javascriptEnabled=TRUE))getURL(paste0(baseURL,"session"),       customrequest="POST",       httpheader=c('Content-Type'='application/json;charset=UTF-8'),       postfields=toJSON(server))serverDetails<-fromJSON(rawToChar(getURLContent('http://localhost:4444/wd/hub/sessions',binary=TRUE)))serverId<-serverDetails$value[[1]]$id

导航到谷歌。

getURL(paste0(baseURL,"session/",serverId,"/url"),       customrequest="POST",       httpheader=c('Content-Type'='application/json;charset=UTF-8'),       postfields=toJSON(list(url="http://www.google.com")))

获取搜索框的ID

elementDetails<-fromJSON(rawToChar(getURLContent(paste0(baseURL,"session/",serverId,"/element"),       customrequest="POST",       httpheader=c('Content-Type'='application/json;charset=UTF-8'),       postfields=toJSON(list(using="xpath",value="//*[@id="gbqfq"]")),binary=TRUE))       )elementId<-elementDetails$value

搜索主题

rawToChar(getURLContent(paste0(baseURL,"session/",serverId,"/element/",elementId,"/value"),       customrequest="POST",       httpheader=c('Content-Type'='application/json;charset=UTF-8'),       postfields=toJSON(list(value=list("uE009","a","uE009",'b','Selenium api in R')))       ,binary=TRUE))

返回搜索HTML

googData<-fromJSON(rawToChar(getURLContent(paste0(baseURL,"session/",serverId,"/source"),       customrequest="GET",       httpheader=c('Content-Type'='application/json;charset=UTF-8'),       binary=TRUE       ))       )

获得建议的链接

gxml<-htmlParse(googData$value)urls<-unname(xpathSApply(gxml,"//*[@]/@href"))

关闭会议

getURL(paste0(baseURL,"session/",serverId),       customrequest="DELETE",       httpheader=c('Content-Type'='application/json;charset=UTF-8')       )


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/452333.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号