在RSelenium中生成给定webElement的绝对xpath [R] [英] Generate Absolute xpath of given webElement in RSelenium [R]

查看:48
本文介绍了在RSelenium中生成给定webElement的绝对xpath [R]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试针对RSelenium中的Java硒复制此答案中所做的操作: https://stackoverflow.com/a /27611777/7837376

I'm trying to replicate what was done in this answer for java selenium in RSelenium: https://stackoverflow.com/a/27611777/7837376

我很想能够做这样的事情:

I'd love to be able to do something like this:

#replicating simple RSelenium process getting all //a elements

library(RSelenium)
#start remDr etc. etc.
all_a <- remDr$findElements(using='xpath','//a')
selected_a <- all_a[[10]]

理想情况下,我可以使用下面的组合函数生成selected_a元素的xpath:

Ideally then I could generate the xpath of the selected_a element using the made up function below:

#desired function
getElementXPATH(selected_a) 

我了解可以为同一元素指定许多不同的XPATH,我只是在寻找该元素的唯一xpath标识符,因此该元素的任何唯一xpath就足够了!

I understand that quite a few different XPATH's could be specified for the same element, I'm just looking for a unique xpath identifier for the element, so any unique xpath to the element will suffice!

谢谢!

推荐答案

我个人并不是绝对的xpath狂热者.但是,您可以使用javascript来获得绝对的xpath,而不是使用您的语言编写该函数,该函数运行速度更快并且易于移植.

Personally, I am not a big fan absolute xpath. However, you can get the absolute xpath using javascript rather having the function in your language, which will ran faster and it's easy to port.

这是JavaScript.

Here is the javascript.

// this function will return the absolute xpath of any given element
jsFunction = """window.getAbsoluteXpath =function(el){
    // initialize the variables
    aPath ="";
    // iterate until the tag name is 'HTML'
    while (el.tagName!='HTML'){
        // get parent node
        pEle=el.parentNode;
        // check if there are more than 1 nodes with the same tagname under the parent
        if(pEle.querySelectorAll(el.tagName).length>1){
            //now findout the index of the current child
            cIndex = 0;
            pEle.querySelectorAll(el.tagName).forEach(function(cEle){
               cIndex= cIndex+1;
               // check if iterating ChildNode is equal to current ChildNode
               if(cEle === el){
                 // set the aPath using index
                 aPath = el.tagName + "[" + cIndex + "]" + "/" +aPath;
               }
            })

        }else{
            // simply add the tagName when there is only one child with the tag name
             aPath = el.tagName + "/" +aPath;
        }
        // set parent node as current element
        el=el.parentNode;
    }
    // append HTML to the absolute xpath generated
    return "//HTML/"+aPath.substring(0,aPath.length-1);
};"""

现在,您可以在javascript中调用此方法,并传递您对获取绝对xpath感兴趣的元素.

Now you can call this method in your javascript and pass element that you are interested in getting the absolute xpath.

让我们尝试获取.

注意:由于我的计算机上缺少环境,因此未测试以下代码逻辑.

# run the javascript in browser so that you can call the function anytime in your script
remDr %>% executeScript(jsFunction, args = list())

# get stackoverflow `Achievements` link element
webElem <- remDr %>% findElement("css", "a.-link.js-achievements-button")
# # get the absolute xpath of Stackoverflow `Achievements`
remDr %>% executeScript("return getAbsoluteXpath(arguments[0])", args = list(webElem))

截屏:在chrome浏览器控制台中运行javascript以获取证据

这篇关于在RSelenium中生成给定webElement的绝对xpath [R]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆