简体   繁体   English

如何在Selenium Webdriver中使用Java验证HTML代码中是否存在用于下拉列表的标记

[英]How to verify whether a tag is present in the html code for a drop down using java in Selenium Webdriver

I have a disabled drop down with Values auto populated as True or False. 我有一个禁用的下拉列表,其中“值”自动填充为True或False。 For the auto populated value, I can see a tag named "selected" when I do the inspect element.How can I verify whether the tag "Selected" is present for the drop down? 对于自动填充的值,执行检查元素时可以看到名为“ selected”的标签。如何验证下拉列表中是否存在“ Selected”标签?

below is the HTML part 以下是HTML部分

<select name="text" class="Product_Selected" disabled>
<Option value="Flag_True" selected>TRUE </option>
<Option value="Flag_False">False </option> ==$0
</select>

As you can see above, I have selected my previous input as TRUE, so next time im getting the drop down auto populated with TRUE and is DISABLED. 正如您在上面看到的那样,我将之前的输入选择为TRUE,因此下次我将使用TRUE自动填充下拉列表并且将其禁用。 Is there any way where I can see whether tag "selected" is present for that disabled drop down using JAVA code for Selenium Webdriver 有什么办法可以使用针对Selenium Webdriver的JAVA代码查看该禁用下拉列表是否存在标签“ selected”

OR Can I get the auto populated value of the Disabled Dropdown? 或能否获得“禁用的下拉菜单”的自动填充值?

You don't have to do anything complicated... just treat it like any other SELECT element. 您不必做任何复杂的事情……就像对待其他任何SELECT元素一样。 There is a special class in Selenium designed to make it easier to interact with SELECT elements called... Select . Selenium中有一个特殊的类,旨在使与SELECT元素(... Select交互更加容易。 I just tested this code with true and false selected and it works just fine even though the element is disabled. 我只是在选择了true和false的情况下测试了此代码,即使禁用了该元素,它也可以正常工作。

Select e = new Select(driver.findElement(By.cssSelector("select.Product_Selected")));
System.out.println(e.getFirstSelectedOption().getText());

You get your SELECT element and send it to the Select constructor. 您将获得SELECT元素,并将其发送到Select构造函数。 Then you can interact with the Select element with all the new functionality. 然后,您可以与具有所有新功能的Select元素进行交互。 The example above just gets the selected option (first selected option in the case of a multi-select, but that doesn't apply here) and returns the text displayed, eg TRUE. 上面的示例仅获取选定的选项(如果是多选,则是第一个选定的选项,但不适用于此处),并返回显示的文本,例如TRUE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Java使用Selenium WebDriver验证是否单击了按钮 - How to verify whether a Button is clicked or not with Selenium WebDriver using Java 如何在Java中使用Selenium验证具有下拉菜单的按钮 - How to verify the button having drop down menu using selenium in java 使用Selenium WebDriver Java处理下拉列表 - Handling drop down using selenium webdriver java 如何从下拉列表中随机选择Selenium Webdriver(java)中的元素在html中? - How to select element from drop down Randomly in Selenium webdriver (java) drop down is in html? 如何在Selenium WebDriver中验证动态下拉值? - How to verify Dynamic drop down values in Selenium WebDriver? 如何使用Java验证Selenium WebDriver中的tooltipText - How to verify tooltipText in Selenium WebDriver using Java 如何使用 Selenium WebDriver 验证元素中是否存在属性? - How to verify an attribute is present in an element using Selenium WebDriver? 如何使用Selenium Webdriver验证模式对话框中的文本 - How to verify the text present in the modal dialog using Selenium Webdriver 如何使用Java使用Selenium Webdriver在此html中获取标记值? - How to get the <b> tag value in this html with Selenium Webdriver using Java? 如何使用 Java 在 Selenium WebDriver 中计算 HTML 子标签 - How to count HTML child tag in Selenium WebDriver using Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM