如果是条件,如果存在id =“prog_bg”,则执行剩余的代码

时间:2013-01-22 09:39:47

标签: webdriver

我想在id="prog_bg"class="progress_box_bg"存在时设置if条件,然后执行剩余的代码。 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())
  6. 谢谢, 萨钦

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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);

答案 2 :(得分:0)

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

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

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

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

相关问题