如何通过xpath检索下拉列表中的文本 [英] How to retrieve the text inside dropdown list by xpath

查看:530
本文介绍了如何通过xpath检索下拉列表中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为网页元素指定xpath.

I want to specify xpath to an element of my web page.

<select id=groupSelect>
   <option value="data" >First value</option>
    <option value="data" >second value</option>
</select>

我想获取第一个值",即选项内的文本.但我不知道如何获取文本.

I want to get the "First value" which is the text inside option. but I do not know how to get the text.

By.xpath("//select[@id='groupSelect']/option[@value=???']"))

推荐答案

selenium可以以下是通过可见文本选择选项的方法(以Java为例):

Here's how you can select the option by visible text (example in java):

Select select = new Select(driver.findElement(By.id("groupSelect")));
select.selectByVisibleText('First value');


如果您仍然希望拥有基于xpath的解决方案,则可以选中选项valuetext:

By.xpath("//select[@id='groupSelect']/option[@value='data' and . = 'First value']")

或通过索引获取它:

By.xpath("//select[@id='groupSelect']/option[1]")

或者您可以同时检查两者.

or you can check both.

这篇关于如何通过xpath检索下拉列表中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆