我想在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">
我尝试了很多选项,但它不起作用。伙计们让我知道它是怎么做到的?
if (var.equals(driver.findElement(By.tagName("body")).getText().contains("prog_bg")))
try { if (selenium.getHtmlSource().matches("^[\\s\\S]*prog_bg[\\s\\S]*$")) break; } catch (Exception e) {};
if(driver.getPageSource().matches("^[\\s\\S]*prog_bg[\\s\\S]*$"))
if(driver.findElement(By.id("prog_bg")).isDisplayed())
if (driver.findElement(By.className("progress_box_bg")).isDisplayed())
谢谢, 萨钦
答案 0 :(得分:0)
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中那样找到这些元素的尝试都是徒劳的。