我如何从WebElement获取xpath

问题描述 投票:0回答:4

如何从 WebElement 获取 xpath

Webelement element= driver.findElement(By.xpath("//div//input[@name='q']"));

类似的东西

element.getLocator(); ->> this should be like this "//div//input[@name='q']"

如何做同样的事情? 我创建了下面的方法并传递 xpath 作为参数。我想创建相同的方法并传递 webElement 作为参数:

public boolean isElementPresentAndVisible(String xpath){
    if(driver.findElements(By.xpath(xpath)).size()!=0){
        if(driver.findElement(By.xpath(xpath)).isDisplayed()){
            System.out.println(xpath+" present and Visible");
            return true;
        }else{
            System.err.println(xpath+" present but NOT Visible");
            return false;
        } 
    }else{
        System.err.println(xpath+" NOT present");
        return false;
    } 

}
selenium xpath
4个回答
3
投票

通过id或name获取WebElement,然后调用方法generategenerateXPATH()传递该元素。它将返回Xpath。找到代码:

 String z=generateXPATH(webElement, "");//calling method,passing the element

 public static String generateXPATH(WebElement childElement, String current) {
        String childTag = childElement.getTagName();
        if(childTag.equals("html")) {
            return "/html[1]"+current;
        }
        WebElement parentElement = childElement.findElement(By.xpath("..")); 
        List<WebElement> childrenElements = parentElement.findElements(By.xpath("*"));
        int count = 0;
        for(int i=0;i<childrenElements.size(); i++) {
            WebElement childrenElement = childrenElements.get(i);
            String childrenElementTag = childrenElement.getTagName();
            if(childTag.equals(childrenElementTag)) {
                count++;
            }
            if(childElement.equals(childrenElement)) {
                return generateXPATH(parentElement, "/" + childTag + "[" + count + "]"+current);
            }
        }
        return null;
    }

0
投票

没有方法可以完成您所要求的操作,但无论如何您也不需要它。你让事情变得比需要的更加复杂。如果某个元素可见,则它也存在,因此无需检查是否存在。

不要将 XPath 作为字符串传递,而是传递

By
定位器。现在您可以使用任何您想要的定位器类型。我重写并简化了您的代码,然后编写了接受
WebElement
参数的类似函数。

public boolean isVisible(By locator)
{
    List<WebElement> e = driver.findElements(locator);
    if (e.size() != 0)
    {
        return isVisible(e.get(0));
    }

    return false;
}

public boolean isVisible(WebElement e)
{
    try
    {
        return e.isDisplayed();
    }
    catch (Exception)
    {
        return false;
    }
}

0
投票

@shamsher Khan 答案的 Python 版本——它有效。

def gen_xpath(child: WebElement, curr: str=''):
    if (child.tag_name == 'html'): return f'/html[1]{curr}'

    parent = child.find_element(By.XPATH, '..')
    children = parent.find_elements(By.XPATH, '*')
    i = 0
    for c in children:
        if c.tag_name == child.tag_name: i += 1
        if c == child: return gen_xpath(parent, f'/{child.tag_name}[{i}]{curr}')

0
投票

我找到了一个简单的解决方案

WebElement element1 = driver.findElement(By.xpath("//*[@id=\"APjFqb\"]"));
String xpath1 = element1.toString();
String[] data1 = xpath1.split(" -> xpath: ",2);
String final_xpath = data1[1].replace("\"]]]", "\"]");
System.out.println(final_xpath);                
© www.soinside.com 2019 - 2024. All rights reserved.