java - 在 selenium java 中使隐藏元素可显示和可点击
问题描述
我有以下元素:
<table class="dijit " data-dojo-attach-point="_buttonNode" cellspacing="0" cellpadding="0" role="lbox" aria-haspopup="true" tabindex="0" id="POS_domain" data-id="domain" widgetid="POS_domain" aria-expanded="false" aria- invalid="false" style="user-select: none;" popupactive="true" aria-owns="POS_domain">
<tbody role="presentation">
<tr role="presentation">
<td class="dijitReset" role="presentation">
<div class="dijitReset Text" data-dojo-attach-point="container" role="presentation">
<span role="option" aria-selected="true" class="dijitLabel ">adrija</span>
</div>
<div class="dijitContainer">
<input class="dijitInner" value="Χ " type="text" tabindex="-1" readonly="readonly" role="presentation">
</div>
<input type="hidden" data-dojo-attach-point="vn" value="adrija" hidden="true">
</td>
<td class="dijitArrowButtonContainer" data-dojo-attach-point="titleNode" role="presentation">
<input class="dijitInner" value="▼ " type="text" tabindex="-1" readonly="readonly" role="presentation">
</td>
</tr>
</tbody>
</table>
上面的元素是下拉元素,是隐藏的。我写的代码是:
private WebElement domainDropdown = Driver.driver.findElement(By.id("POS_domain"));
domainDropdpwn.click();
private WebElement adrija = Driver.driver.findElement(By.xpath("//input[@value='adrija' and @data-dojo-attach-point='vn']"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", adrija);
它说它无法找到该元素。
请帮忙。谢谢。:)
解决方案
所需的<input>
标签具有属性type="hidden"
和hidden="true"
,因此click()
在元素上您可以使用以下解决方案:
//driver being an instance of WebDriver
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//table[@class='dijit ' and @id='POS_domain']"))).click();
WebElement my_adrija = driver.findElement(By.xpath("//input[@value='adrija' and @data-dojo-attach-point='vn']"));
((JavascriptExecutor)driver).executeScript("arguments[0].removeAttribute('hidden')", my_adrija)
((JavascriptExecutor)driver).executeScript("arguments[0].setAttribute('type','text')", my_adrija)
WebElement my_new_adrija = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@value='adrija' and @data-dojo-attach-point='vn']")));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", my_new_adrija);
推荐阅读
- node.js - 如何在 NodeJS 项目中自动“审核”或“合理化”我的导入?
- xpath - Java XPath 前缀必须解析为命名空间
- javascript - 为什么这个来自 MDN 的简单 AJAX 示例会失败?
- python - 如何关闭与 Azure 服务总线队列的连接?
- java - 解析节点内具有相同属性的 XML 文件
- javascript - 如何在下拉列表中获取默认值?
- directory - 无法让 THREE.FBXLoader 定位到纹理文件夹
- javascript - 有没有办法用 p5.js 在多点场景中跟踪点的路径
- reactjs - 在 React 中重定向到具有不同页面布局的页面
- javascript - 在反应渲染中解析来自firebase的数据