简体   繁体   English

将 WebElement 类型转换为 By 类型

[英]Converting a WebElement type to a By type

Is there any way to convert a WebElement type object to a By in selenium?有什么方法可以将WebElement类型的对象转换为 selenium 中的By Type casting did not work.类型转换不起作用。

I have a function which only takes in By's, so I need to convert a WebElement to a By .我有一个只接受 By 的函数,所以我需要将WebElement转换为By

If you are using a method which takes an argument of a WebElement, it is likely that you are considering the problem in a way that would be less effective than if you passed it a by selector instead. 如果您正在使用一个接受WebElement参数的方法,那么您可能会以一种效率低于通过选择器传递它的方式来考虑问题。 However, this method should fulfill your ask. 但是,这种方法应该满足您的要求。

// return ByType of WebElement
public By toByVal(WebElement we) {
    // By format = "[foundFrom] -> locator: term"
    // see RemoteWebElement toString() implementation
    String[] data = we.toString().split(" -> ")[1].replace("]", "").split(": ");
    String locator = data[0];
    String term = data[1];

    switch (locator) {
    case "xpath":
        return By.xpath(term);
    case "css selector":
        return By.cssSelector(term);
    case "id":
        return By.id(term);
    case "tag name":
        return By.tagName(term);
    case "name":
        return By.name(term);
    case "link text":
        return By.linkText(term);
    case "class name":
        return By.className(term);
    }
    return (By) we;
}

This could help. 这可能有所帮助。

private static WebElement returnElement(String stringElement) {
    if (stringElement == null) {
        return null;
    } else if (stringElement.contains("->")) {
        String[] splitElement = stringElement.split(" ");
        String locatorType = splitElement[6].replace(":", "");
        String locatorPath = null;
        if(locatorType.equalsIgnoreCase("xpath")) {
            locatorPath = stringElement.substring(stringElement.indexOf("//"), stringElement.length() -1);
        } else {
            locatorPath = splitElement[7].substring(0, splitElement[7].length() - 1);
        } 
        return returnElement(locatorType, locatorPath);
    } else {
        String[] splitElement = stringElement.split(" ");
        String locatorType = splitElement[4].substring(splitElement[4].lastIndexOf(".") + 1,
                splitElement[4].lastIndexOf(":"));
        int index = splitElement[5].length();
        String locatorPath = splitElement[5].substring(0, index - 1);
        return returnElement(locatorType, locatorPath);
    }
}

// Refreshing DOM Elements
private static WebElement returnElement(String locatorType, String locatorPath) {
    switch (locatorType.toLowerCase()) {
    case "id":
        return driver.findElement(By.id(locatorPath));

    case "xpath":
        return driver.findElement(By.xpath(locatorPath));

    case "name":
        return driver.findElement(By.name(locatorPath));

    case "classname":
        return driver.findElement(By.className(locatorPath));

    case "cssselector":
        return driver.findElement(By.cssSelector(locatorPath));

    case "linktext":
        return driver.findElement(By.linkText(locatorPath));

    case "tagname":
        return driver.findElement(By.tagName(locatorPath));

    default:
        throw new RuntimeException("Unknown locator " + locatorType + " : " + locatorPath);
    }
}

I use this method and it works fine for me.我使用这种方法,对我来说效果很好。 To run this code block it requires Apache Commons IO library.要运行此代码块,它需要Apache Commons IO库。

 /**
     * Converting WebElement to By locator
     *
     * @param webElement
     * @return By
     */
    public static By getByLocator(WebElement webElement) {
        String element = webElement.toString().split(
                "(?=\\sid:\\s|\\sname:\\s|\\sselector:\\s|\\slink text:|\\sxpath:\\s|" +
                        "By.id:\\s|By.name:\\s|By.tagName:\\s|By.className:\\s|By.cssSelector:\\s|" +
                        "By.linkText:\\s|By.partialLinkText:\\s|By.xpath:\\s)")[1];

        String[] locator = StringUtils.removeEnd(element, "]").split(":\\s");
        String method = locator[0].trim();
        if (method.equals("xpath"))
            return By.xpath(locator[1]);

        String selector = StringUtils.removeEnd(locator[1], "'");
        switch (method) {
            case "id":
            case "By.id":
                return By.id(selector);
            case "name":
            case "By.name":
                return By.name(selector);
            case "By.tagName":
                return By.tagName(selector);
            case "By.className":
                return By.className(selector);
            case "selector":
            case "By.cssSelector":
                return By.cssSelector(selector);
            case "By.linkText":
                return By.linkText(selector);
            case "link text":
            case "By.partialLinkText":
                return By.partialLinkText(selector);
            case "By.xpath":
                return By.name(selector);
            default:
                System.out.println("Error! [" + method + "]");
                return null;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 转换列表 <WebElement> 到WebElement - Converting List<WebElement> to WebElement Selenium webdriver: <WebElement> 迭代器无法解析为某种类型 - Selenium webdriver : <WebElement> Iterator cannot be resolved to a type 错误:对于类型webelement,方法selectByVisibleText(string)未定义 - Error: The method selectByVisibleText(string) is undefined for the type webelement 未定义类型 List 的方法 sendKeys(String)<WebElement> - The method sendKeys(String) is undefined for the type List<WebElement> 类型转换Webelement to Select时出错 - Error while type casting Webelement to Select 对于WebElement类型,未定义方法sendkeys(int) - The method sendkeys(int) is undefined for the type WebElement 类型列表 web 元素的方法 gettext() 未定义 - The method gettext() is undefined for the type list webelement 出现错误“表达式类型必须是数组类型,但它被解析为 Llist<webelement> ”</webelement> - Getting error “type of expression must be an array type but it is resolved to Llist<WebElement>” 将类型对象转换为类型JButton? - Converting a type Object to a type JButton? WebElement类型的方法sendKeys(CharSequence…)不适用于参数(双精度) - The method sendKeys(CharSequence…) in the type WebElement is not applicable for the arguments (double)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM