有什么方法可以转换 WebElement将对象输入到 By在 Selenium ?类型转换不起作用。

我有一个只接受 By 的函数,所以我需要转换一个 WebElementBy .

请您参考如下方法:

如果您使用的方法接受 WebElement 的参数,则您考虑问题的方式可能不如通过选择器传递它更有效。
但是,这种方法应该可以满足您的要求。

// 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; 
} 


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!