Reputation: 1912
Im trying the code below but it seems it does not work... Can someone show me the best way to do this?
public void verifyThatCommentDeleted(final String text) throws Exception {
new WebDriverWait(driver, 5).until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver input) {
try {
input.findElement(By.xpath(String.format(
Locators.CHECK_TEXT_IN_FIRST_STATUS_BOX, text)));
return false;
} catch (NoSuchElementException e) {
return true;
}
}
});
}
Upvotes: 38
Views: 207000
Reputation: 17
My answer is much more simpler and it works for me
while(true) {
if (driver.getPageSource().contains("Enter the text of the element you wanted to check")==false)
{//Put your code here
break;
}
else if (isElementPresent(element you are trying to find)==true)
{
//put your code here
break;
}
}
Upvotes: 0
Reputation: 4212
In Python for assertion I use:
assert len(driver.find_elements_by_css_selector("your_css_selector")) == 0
Upvotes: 0
Reputation: 1868
public boolean isDisplayed(WebElement element) {
try {
return element.isDisplayed();
} catch (NoSuchElementException e) {
return false;
}
}
If you wan t to check that element is displayed on the page your check should be:
if(isDisplayed(yourElement){
...
}
else{
...
}
Upvotes: 2
Reputation: 15
WebElement element = driver.findElement(locator);
Assert.assertFalse(element.isDisplayed());
The assertion will pass if the element is not present, otherwise it will fail.
Upvotes: -3
Reputation: 200
WebElement element = driver.findElement(locator);
Assert.assertNull(element);
The above assertion will pass if element is not present.
Upvotes: 0
Reputation: 109
Use findElements instead of findElement.
findElements will return an empty list if no matching elements are found instead of an exception. Also, we can make sure that the element is present or not.
Ex: List elements = driver.findElements(By.yourlocatorstrategy);
if(elements.size()>0){
do this..
} else {
do that..
}
Upvotes: 7
Reputation: 1
int i=1;
while (true) {
WebElementdisplay=driver.findElement(By.id("__bar"+i+"-btnGo"));
System.out.println(display);
if (display.isDisplayed()==true)
{
System.out.println("inside if statement"+i);
driver.findElement(By.id("__bar"+i+"-btnGo")).click();
break;
}
else
{
System.out.println("inside else statement"+ i);
i=i+1;
}
}
Upvotes: 0
Reputation: 51
Unable to comment to The Meteor Test Manual, since I have no rep, but I wanted to provide an example that took me quite awhile to figure out:
Assert.assertEquals(0, wd.findElements(By.locator("locator")).size());
This assertion verifies that there are no matching elements in the DOM and returns the value of Zero, so the assertion passes when the element is not present. Also it would fail if it was present.
Upvotes: 5
Reputation: 7339
i usually couple of methods (in pair) for verification whether element is present or not:
public boolean isElementPresent(By locatorKey) {
try {
driver.findElement(locatorKey);
return true;
} catch (org.openqa.selenium.NoSuchElementException e) {
return false;
}
}
public boolean isElementVisible(String cssLocator){
return driver.findElement(By.cssSelector(cssLocator)).isDisplayed();
}
Note that sometimes selenium can find elements in DOM but they can be invisible, consequently selenium will not be able to interact with them. So in this case method checking for visibility helps.
If you want to wait for the element until it appears the best solution i found is to use fluent wait:
public WebElement fluentWait(final By locator){
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(locator);
}
});
return foo;
};
Hope this helps)
Upvotes: 41
Reputation: 2533
Instead of doing findElement, do findElements and check the length of the returned elements is 0. This is how I'm doing using WebdriverJS and I expect the same will work in Java
Upvotes: 57