检查文本是否可见Selenium + Java

时间:2018-08-06 12:20:04

标签: java selenium-webdriver

这是HTML:

<select size="1" id="F_21__3" class="c3" style="position: absolute; left: 127px; top: 173px; width: 148px; height: 14px; font-style: normal; font-weight: normal; text-decoration: none; text-shadow: none; font-stretch: normal;" disabled="disabled">
    <option value="I" des="Item">Item</option>
    <option value="S" des="Service">Service</option>
</select>

img

如何使用Selenium + Java检查文本“ Service”是否可见?

5 个答案:

答案 0 :(得分:3)

用于检查是否选择了“服务”的代码:

+--------+-------------------+---------+------------+-------+----------+-------------------+
|   ResId|      vrsn_strt_dts|instnc_nm|ship_strm_nb|vyge_id|onhand_cnt|       vrsn_end_dts|
+--------+-------------------+---------+------------+-------+----------+-------------------+
|27608171|2018-07-17 04:00:00| Standard|         11B|   1000|        10|2018-07-17 04:00:00|
|27608171|2018-09-17 04:00:00| Standard|         11B|   1000|        40|2018-09-17 04:00:00|
|27608171|2018-09-18 04:00:00| Standard|         11B|   1000|        10|2018-09-18 04:00:00|
|27608171|2018-09-19 04:00:00| Standard|         11B|   1000|        10|2018-09-19 04:00:00|
|27608174|2018-08-17 04:00:00| Standard|         11C|   2000|        20|2018-08-17 04:00:00|
|27608174|2018-09-17 04:00:00| Standard|         11D|   5000|        50|2018-09-17 04:00:00|
|27608173|2018-09-17 04:00:00| Standard|         11D|   3000|        30|2018-09-17 04:00:00|
+--------+-------------------+---------+------------+-------+----------+-------------------+

答案 1 :(得分:0)

用于检查文本“服务”是否可见的代码

 String checkTextVisible = driver.findElement(By.xpath("//*[contains(text(),'Service')]").getText();
if(checkTextVisible.equals("Service")){
   System.out.println(checkTextVisible + " text is visible");
}

答案 2 :(得分:0)

禁用或启用该字段并不重要。您只需找到正确的选择器,然后您就可以非常轻松地进行检查。

String actualText = driver.findElemeny(By.cssSelector("yourSelector")).getAttribut("value");
Assert.assertEqual(actualText, "Service");

或更简单(但前提是“服务”未出现在页面的其他位置)。

boolean presence = false;
if(driver.getPageSource().contains("Service")){
System.out.println("Text is present");
presence = true;
}else{
System.out.println("Text is absent");}

Assert.assertTrue(presence);

答案 3 :(得分:0)

您可以使用getFirstSelectedOption()来显示可见的选定下拉列表。

@Findby(id="F_21__3")
private WebElement selectID;

Select s = new Select(selectID);
   String selected = s.getFirstSelectedOption().getText();
   Assert.assertTrue(selected.equals("Service"));

答案 4 :(得分:-3)

如果要检查是否选择了“服务”,则下面的代码将返回true / false:

driver.findElement(By.xpath("//select[@id='F_21__3']/option[.='Service']")).isSelected()