java - Selenium 单击非按钮元素
问题描述
所以我试图点击一个不是按钮元素的元素,html代码如下:
<div class="menu-item-header">
<span class="header-title"><span class="icon-wrapper"><i class="icon-helper"></i></span>Help
</span> <!--When a client clicks this it redirects them to the appropriate place (this is a React website)-->
</div>
原始网站看起来像这样:
<div class="menu-item-header">
<span class="header-title"><span class="icon-wrapper"><i class="icon-helper"></i></span>Help
</span> <!---->
</div>
<div class="menu-item-header">
<span class="header-title"><span class="icon-wrapper"><i class="icon-helper2"></i></span>Help2
</span> <!---->
</div>
...
我尝试了以下方法来单击元素:
WebElement element = browser.findElement(...);
element.click();
然而这并没有奏效。
解决方案
所需的元素是一个React元素,因此要单击带有文本作为帮助的元素,您需要诱导WebDriverWait并且elementToBeClickable()
您可以使用以下基于xpath的定位器策略之一:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='menu-item-header']/span[@class='header-title' and contains(., 'Help')]"))).click();
推荐阅读
- python - 有没有办法将假设存储为向量的字符串转换为向量?
- excel - 将字符串连接到vba中的一系列单元格?
- c++ - C++中显式删除move构造函数的用例
- python - 如何在没有第二个文件的情况下更改文件中的多个值?
- python - 比较时间值和日期时间索引
- android - 过滤最后一个元素并添加到可变列表kotlin中的列表顶部
- javascript - 使用 API 调用响应 setState 延迟更新
- python - 通过 Pandas 动画 Matplotlib/Seaborn 情节?
- c# - 在 IIS 上使用 Visual Studio 远程调试器调试 program.cs
- php - 从 json 响应中获取特定值