2014-04-07 2 views
3

WebElement 유형의 객체를 셀렌의 By으로 변환 할 수있는 방법이 있습니까? 유형 전송이 작동하지 않았습니다.WebElement 유형을 By 유형으로 변환

저는 By 's 만 취하는 함수가 있으므로 WebElementBy으로 변환해야합니다.

+0

이미 WebElement를 가져 오기 위해 By라는 기능을 사용하고 계신 것 같습니까? 아마도 메서드를 오버로드하는 방법이 될 것입니다. – donfuxx

+0

나는 그렇게 할 방법이 없다고 생각합니다. 나는 좋은 해결책을 제안하기 위해 당신이 성취하고자하는 것을보아야 할 필요가 있다고 생각합니다. 내 제안은'WebElement'와'By' 속성을 가진 객체를 만들고, 메소드에'element.By'를 전달하는 것입니다. – Richard

+2

javascript를 사용하여 webelement의 절대 xpath를 얻은 다음 그로부터'By' 개체를 만들 수 있습니다. – Faiz

답변

1

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; 
} 
+1

문제 해결 방법 및 해결 방법에 대한 의견을 보내주십시오. –

0

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); 
    } 
} 
관련 문제