鼠标悬停元素时出错

时间:2017-12-06 11:29:34

标签: selenium-webdriver

当鼠标悬停在selenium中包含以下代码的元素时出现错误:

public void our_medicines(String locatorType, String value) {
 try {
  By locator;
  locator = locatorValue(locatorType, value);
  Actions action = new Actions(driver);
  WebDriverWait wait = new WebDriverWait(driver, 10);
  WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
  action.moveToElement(element).click().build().perform();
 } catch (Exception e) {
  System.err.format("no such element to mousehover" + e);
 }
}

这是我的locatorValue方法:

public By locatorValue(String locatorTpye, String value) {
 By by;
 switch (locatorTpye) {
  case "id":
   by = By.id(value);
   break;
  case "name":
   by = By.name(value);
   break;
  case "xpath":
   by = By.xpath(value);
   break;
  case "css":
   by = By.cssSelector(value);
   break;
  case "linkText":
   by = By.linkText(value);
   break;
  case "partialLinkText":
   by = By.partialLinkText(value);
   break;
  case "className":
   by = By.className(value);
   break;
  default:
   by = null;
   break;
 }
 return by;
}

1 个答案:

答案 0 :(得分:0)

好的,我在locatorValue方法中发现的问题是它无法处理此块中未指定的定位器

default: by = null;
 break;
 }
 return by; }

我添加了一些简单的东西来解决这个问题,但你可以根据需要调整它

default: throw new Exception("Locator type not defined");
}
return by; }

确保传递给public void our_medicines的locatorType参数是有效的定位器类型。