简而言之,我需要自动从WebForm中选择下拉值。我尝试使用:
$("td:contains(Gender)")
.closest('td')
.next()
.find('select')
.filter(function () { return $(this).html() == "Female"; })
.val();
(只要看看我是否可以返回任何东西,它说未定义(
和
$("td:contains(Gender)")
.closest('td')
.next()
.find('select')
.find('option:contains(Female)')
.attr('selected', true)
(但这需要嵌套find((才能工作
HTML看起来像:
<html>
<table>
<tr>
<td>Gender:</td>
<td>
<select>
<option>Male</option>
<option>Female</option>
</select>
</td>
</tr>
</table>
</html>
由于WebForm构建的"唯一"方式,因此我无法使用ID或类。我也知道,由于男性和女性都包含"男性",我可能无法使用包含。下拉列表中的值都是数值的,因此我更喜欢使用文本而不是映射值。
如果您想知道我是一名自动化工程师,所以我无法控制表单本身。
善意,
K
您几乎是正确的,您只是不需要致电.closest('td')
,因为$("td:contains(Gender)")
将返回td
本身的jQuery对象。
$("td:contains(Gender)")
.next()
.find('select')
.find('option:contains(Female)')
.attr('selected', true)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<table>
<tr>
<td>Gender:</td>
<td>
<select>
<option>Male</option>
<option>Female</option>
</select>
</td>
</tr>
</table>
</html>