Java Webdriver:元素不可见异常 [英] Java webdriver: Element not visible exception

查看:49
本文介绍了Java Webdriver:元素不可见异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题.我有一个隐藏的下拉菜单,因此当我进行选择并运行测试时,出现以下错误:

I'm having the following problem. I have a dropdown that is hidden so when I make the Select and run the test i get the following error:

 org.openqa.selenium.ElementNotVisibleException: element not visible: Element is not currently visible and may not be manipulated
  (Session info: chrome=30.0.1599.101)

这是我的选择:

Select s = new Select(dropDown);
s.selectByVisibleText("CHARGEBACK");

是否在周围走动以操纵隐藏的元素?我在其中一篇文章中找到了以下代码:

Is there a walk around it to manipulate hidden elements?. I found the following code in one of the posts:

 JavascriptExecutor jse = (JavascriptExecutor) driver;
 jse.executeScript("arguments[0].scrollIntoView(true);", element);

这是html代码:

 <div class="ui-helper-hidden">
<select id="formLevel:levels_input" name="formLevel:levels_input">
<option value="541fac58-5ea8-44ef-9664-e7e48b6c6a3c">Seleccione un Registro</option>
<option value="dafc799c-4d5e-4b02-a882-74cb6ad98902">SECURITY</option>
<option value="e5416086-2036-4cd0-b23e-865747aa3f53">CALL CENTER</option>
<option value="7ea4b4ea-4f06-4d27-9541-1b0cf3f2aa22">CHARGEBACK</option>
<option value="0f915120-7b8f-4a33-b063-5d20a834b655">PREVENÇÃO A FRAUDE</option>
<option value="a8ef13e8-f4a5-43b8-a668-b769f6988565">ANALISE DE CREDITO</option>
<option value="83b65a26-d4cd-43d3-b3fa-2f7894ca454a">SUPORTE A CONTA</option>
<option value="163d0db9-590c-47a7-a271-218b2d27d8d9">REGULARIZAÇÃO FINANCEIRA</option>

在这种情况下,它不起作用.任何帮助将不胜感激.

And it doesn't work in this case. Any help would be appreciated.

推荐答案

由于 WebDriver 试图模拟真实用户,因此它无法与不可见/隐藏的元素进行交互.为了解决您的问题,我认为您首先需要单击 div ,这将使下拉列表可见,并从下拉列表中选择选项.我会推荐这种方法,而不是纯Javascript方法,因为它可以模拟真实用户.试一试,

Since WebDriver tries to simulate real users, it cannot interact with elements which are invisible/hidden. To solve your issue, I think you would need to click on div first which will make the drop down visible and select option from the dropdown. I would recommend such an approach as opposed to pure Javascript way since it would simulate a real user. Give following a shot,

WebDriverWait wait = new WebDriverWait(driver, 300);
WebElement triggerDropDown = driver.findElement(By
                .className("ui-helper-hidden"));
triggerDropDown.click();
WebElement selectElement = wait.until(ExpectedConditions
                  .visibilityOfElementLocated(By.id("formLevel:levels_input")));
Select select = new Select(selectElement);
select.selectByVisibleText("SECURITY");

编辑更新了triggerDropDown的类名称

Edit updated the class name of triggerDropDown

这篇关于Java Webdriver:元素不可见异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆