【问题标题】:Selenium webdriver not able to select element.Getting "Element is not currently visible and so may not be interacted with" errorSelenium webdriver 无法选择元素。获取“元素当前不可见,因此可能无法与之交互”错误
【发布时间】:2016-02-29 12:38:00
【问题描述】:

我在 eclipse、testng 和 firefox 浏览器中使用 selenium webdriver 我正在尝试单击 ID 为“compName”的下拉列表,但无法单击它。 我已经尝试了所有可能的方案来识别 drodpown,但我总是收到此错误“元素当前不可见,因此可能无法与之交互” 它的html代码如下

<div id="login" class="b-container" style="display:block">
<form action="" name="passwordForm" commandname="passwordForm" method="post">
<input id="userVo.email" type="hidden" name="userVo.email">
</form>
<form action="" name="resetForm" commandname="resetForm" method="post"> </form>
<form id="loginForm" class="form-login" onsubmit="return onClickLogin();" name="loginForm" action="" method="post">
<input id="compLocId" type="hidden" name="compLocId" value="1">
<h2 class="form-login-heading">SIGN IN NOW</h2>
<div class="login-wrap">
<div id="invDivMsg" style="color:red;"></div>
<input id="loginId" class="form-control" type="text" placeholder="User ID" name="loginId" disabled="" vk_156ee="subscribed">
<input id="password" class="form-control" type="password" placeholder="Password" name="password" disabled="" vk_156ee="subscribed">
<button id="signIn" class="btn btn-lg btn-info btn-block btn-drop marginLeft23" type="submit" style="width: 80%; display: none;" disabled="">Sign in</button>
<button id="reset" class="btn btn-lg btn-info btn-block btn-drop marginLeft23" onclick="onClickReset()" type="reset" style="width: 80%;">Reset</button>
<div class="select col-md-12">
<select id="compName" onchange="fillLocDrpDown();" name="compName" style="display: block;">
<option value="">Select Company</option>
<option value="5">XYZ ltd</option>
<option value="6">Freight Forwarder Co</option>
<option value="2">CHA</option>
</select>
</div>

欢迎提出任何建议。

【问题讨论】:

  • 它可能被封装在处理点击的容器元素中。尝试单击其中之一。实际的输入元素是隐藏的。我解决这个问题的方法是尝试每一个,直到找到我需要的那个。从输入的第一个父级向上。
  • 所以在这种情况下,请尝试点击&lt;div class="select col-md-12"&gt;。我认为这是您需要的。\
  • 代码你试过帮助你吗?
  • 如果需要的元素默认隐藏,您可以使用JavaScript使其可见。在Python 中,这应该看起来像driver.execute_script("document.getElementById('compName').style.visibility = 'visible';"),但在Java 中,类似的方法看起来更复杂,恐怕我无法帮助您将Python 转换为Java 代码“翻译”

标签: java api selenium selenium-webdriver testng


【解决方案1】:

尝试使用显式等待和预期条件

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("compName")));
element.click();

这将等待最多 10 秒才能显示下拉菜单。

【讨论】:

  • 谢谢。使用上面的代码我可以点击元素。
猜你喜欢
  • 2015-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-27
  • 1970-01-01
  • 2014-11-13
  • 2016-11-13
相关资源
最近更新 更多