Pho*_*One 7 java selenium selenium-webdriver
有没有办法将WebElement类型对象转换为By硒中的类型对象?类型转换不起作用。
我有一个只接受 By 的函数,所以我需要将 a 转换WebElement为 a By。
如果您使用的方法采用 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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16080 次 |
| 最近记录: |