1

我想设置一个 if 条件,当id="prog_bg"orclass="progress_box_bg"存在时,然后执行剩余的代码。DOM如下,

<div id="wizard">
 <div class="quote_header">
 <div class="items">
      <div id="top-line" style="display: block;">
            <div class="back_box">
             <div class="progress_box">
                 <div id="prog_bg" class="progress_box_bg" style="width: 75px;"></div>
             </div>
             </div>
        <div id="service-div" class="page" style="padding-top:45px; >">
        <div id="propertytype-div" class="page">

我尝试了很多选项,但它不起作用。大佬们告诉我怎么弄?

  1. if (var.equals(driver.findElement(By.tagName("body")).getText().contains("prog_bg")))
  2. try { if (selenium.getHtmlSource().matches("^[\\s\\S]*prog_bg[\\s\\S]*$")) break; } catch (Exception e) {};
  3. if(driver.getPageSource().matches("^[\\s\\S]*prog_bg[\\s\\S]*$"))
  4. if(driver.findElement(By.id("prog_bg")).isDisplayed())
  5. if (driver.findElement(By.className("progress_box_bg")).isDisplayed())

谢谢,萨钦

4

3 回答 3

0

你能试试这样的东西吗?您可以用 if() 替换 Assert

boolean ispresent= driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*prog_bg:[\\s\\S]*$");//TODO: Make this dynamic
Assert.assertEquals(true, ispresent);

boolean ispresent= driver.findElement(By.id("prog_id"))
Assert.assertEquals(true, ispresent);
于 2013-01-23T22:16:19.587 回答
0

你也可以尝试这样的事情。

// Find element by id
boolean isElementPresent = driver.findElement(By.id("prog_id"));
// Find element by class
boolean isElementPresent = driver.findElement(By.className("progress_box_bg"));
//This  writes out an errormessage if isElementPresent equals false, good for reports!
Assert.assertTrue("Could not find the element", isElementPresent);
于 2013-05-08T12:38:59.370 回答
0

数字 4 和 5 在正确的轨道上。isDisplayed()但是,如果您只想检查它在 DOM 中的存在,则不应调用。isDisplayed()检查 DOM 中的元素是否存在,然后检查该元素是否对用户可见

相反,您应该尝试查找元素本身:

if(driver.findElement(By.id("prog_bg")) || driver.findElement(By.className("progress_box_bg")) {
/*Execute code here*/
}

另外,请注意元素属性不会出现在页面正文的文本中,只会出现在 DOM 中。任何像你在数字 1、2 和 3 中那样寻找这些元素的尝试都是徒劳的。

于 2013-05-08T17:47:02.573 回答