c# - C# Selenium 如何在特定文本旁边找到下拉列表?
问题描述
我正在尝试测试包含多个下拉列表的页面,这些下拉列表在每次刷新时在页面中具有动态 ID 和动态位置。
例如,我想首先找到与标签 5 对应的下拉列表并选择特定值,所以我想通过它旁边的标签找到下拉列表。
我试过这个:
driver.FindElement(By.XPath("//span[text()='Label 5']/following::div[@class='select custom-select custom-select m-l-1']"));
但我收到“无法定位”错误。
编辑:添加下拉列表 HTML 代码:
<tr class="r0">
<td class="text">
<p>Label 5
<br>
</p>
</td>
<td class="control">
<label class="accesshide" for="menuq6803:2_sub0">Answer 1</label>
<select class="select custom-select custom-select m-l-1" id="menuq6803:2_sub0" name="q6803:2_sub0">
<option value="0">Choose...</option>
<option value="1">This is answer 1</option>
<option selected="selected" value="2">This is answer 2</option>
</select>
</td>
</tr>
解决方案
这里有几个应该可以工作的 XPath
//p[contains(text(),'Label 5')]/following::select
或者
//tr[.//p[contains(text(),'Label 5')]]/td/select
第二个更安全,因为它要求 Label 和 SELECT 都在同一个 TR 下。
推荐阅读
- vbscript - 配置多个帐户时如何自动化 Outlook?
- opengl - OpenGL 使用着色器将 NV12 转换为 RGB24
- html - 如何将函数的输出作为对象读取
- javascript - 使用正则表达式删除 jquery 字符串中的单引号和最后一个逗号
- angular - 如何将列表组件中的数据传递给编辑组件以进行数据修改
- javascript - TypeError:在 props.onStartGame(selectedNumber) 中,'props.onStartGame' 未定义
- ssl - Tomcat 8 不强制 SSL 相互身份验证
- docker - 在 docker 中执行 ADD cmd 时创建的 tmp/setup 文件夹在哪里?
- java - 如何在 Java 中创建包含泛型的自定义类数组
- php - 如何为资源路由的路由指定名称