有没有办法测试元素是否存在?任何findElement方法都会以异常结束,但这不是我想要的,因为它可能是一个元素不存在而且没关系,这不是测试的失败,因此异常不能成为解决方案.
我发现这篇文章:Selenium c#Webdriver:等到元素存在 但是这是C#而我不是很擅长.任何人都可以将代码翻译成Java吗?我很抱歉,我在Eclipse中尝试过但是我没有把它直接用到Java代码中.
这是代码:
public static class WebDriverExtensions{
public static IWebElement FindElement(this IWebDriver driver, By by, int timeoutInSeconds){
if (timeoutInSeconds > 0){
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(drv => drv.FindElement(by));
}
return driver.FindElement(by);
}
}
Run Code Online (Sandbox Code Playgroud) 如何过滤具有相同类的元素?
<html>
<body>
<p class="content">Link1.</p>
</body>
<html>
<html>
<body>
<p class="content">Link2.</p>
</body>
<html>
Run Code Online (Sandbox Code Playgroud) 编辑:所以我想出了一种悬停在元素上的简单方法,但我想等待弹出一个结果.Chrome网页驱动程序悬停在元素上并且移动得太快,以至于我无法看到文本.在文本弹出之前,如何让它保持悬停状态?我查看了Wait()和until(),但我似乎无法使它们正常工作(我认为这是因为我并不是真的在等待代码中的布尔值.除非有人有一些建议吗? ).这就是我到目前为止所拥有的......
WebDriver driver = getWebDriver();
By by = By.xpath("//*[@pageid='" + menuItem + "']");
Actions action = new Actions(driver);
WebElement elem = driver.findElement(by);
action.moveToElement(elem);
action.perform();
Run Code Online (Sandbox Code Playgroud)
再次感谢大家!
干杯.
我正在编写一个测试,我想验证页面上不存在某个元素(显示或以其他方式)。我读过各种文章(例如这篇文章)如何使用空列表或非空列表进行元素检测。这对于验证元素是否存在的相反测试来说效果很好。但是,当该元素不存在时,我在旋转 60 秒后始终会收到 WebDriverException 超时:请参阅此处的屏幕截图
元素检测函数如下:
public bool isButtonPresent(string buttonType)
{
switch (buttonType)
{
case "Button 1":
return !(Driver.FindElements(By.Id("Button 1 ID Here")).Count == 0);
case "Button 2":
return !(Driver.FindElements(By.Id("Button 2 ID Here")).Count == 0);
case "Button 3":
return !(Driver.FindElements(By.Id("Button 3 ID Here")).Count == 0);
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
感谢您的时间!
我知道我可以用 (driver.findElements(By.xpath("Xpath Value")).size() != 0);
但是,我使用的是页面对象模型,其全部目的是在单独的类中预定义WebElement,因此不必在测试类中使用“ FindElements By”。
这是我目前拥有的
if (objPage.webElement.isEnabled()){
System.out.println("found element");
}else{
System.out.println("element not found");
}
Run Code Online (Sandbox Code Playgroud)
但是,这试图识别可能不存在的WebElement。当它不存在时,我得到:
没有此类元素”例外。
是override WebElement click()添加一些wait功能的好习惯,因为在某些页面中我需要单击按钮,并且在某些情况下还没有加载按钮,因此我添加了等待以检查元素是否可见。
所以我的问题是:创建abstract class一个实现WebElement并重写click()方法以添加一些等待功能的更好的方法,还是仅在特定页面中进行纯等待的方法更好?
我想知道List当我试图找到一些WebElement但没有找到时,如何返回空.当然我想避免崩溃,所以这就是我尝试过的:
def getList(): List[WebElement] = {
try {
driver.fineElements(By.xpath("bla bla))
}catch{
case e: TimeoutException => // What should i put here ???
}
}
Run Code Online (Sandbox Code Playgroud)