Check if an item is available in Selenium Java

I am new to Seleniumand have to check to see if an item is available in Selenium Java, since it element.click()passes both on linkand on label.

I tried using the code below but did not work:

WebDriverWait wait = new WebDriverWait(Scenario1Test.driver, 10);

if(wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@id='brandSlider']/div[1]/div/div/div/img)[50]")))==null)

You need help.

+10
source share
7 answers

elementToBeClickable used to verify that an element is visible and turned on so that you can click it.

ExpectedConditions.elementToBeClickablereturns WebElementif the expected condition is true, otherwise it will throw TimeoutException, it never returns null.

, ExpectedConditions.elementToBeClickable , clickable, null, : -

WebDriverWait wait = new WebDriverWait(Scenario1Test.driver, 10); 
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@id='brandSlider']/div[1]/div/div/div/img)[50]")));
element.click();

, element.click() link, label, , , clicked, ,.

: - id, name, className . , cssSelector xpath, , , .

, ..:)

+14

, element.isDisplayed() && element.isEnabled() true, , / - .

Exception :

org.openqa.selenium.WebDriverException: : (781, 704). : <div class="footer">...</div>

:

WebElement  element=driver.findElement(By.xpath"");  
JavascriptExecutor ex=(JavascriptExecutor)driver;
ex.executeScript("arguments[0].click()", element);

.

+11

wait.until(ExpectedConditions) null, , TimeoutException.

,

WebElement element = driver.findElement(By.xpath);
if (element.isDisplayed() && element.isEnabled()) {
    element.click();
}
+4

, ExpectedConditions.elementToBeClickable(), , isEnabled() isDisplayed(). .

public static ExpectedCondition<WebElement> elementToBeClickable(final WebElement element) {
		return new ExpectedCondition() {
			public WebElement apply(WebDriver driver) {
				WebElement visibleElement = (WebElement) ExpectedConditions.visibilityOf(element).apply(driver);

				try {
					return visibleElement != null && visibleElement.isEnabled() ? visibleElement : null;
				} catch (StaleElementReferenceException arg3) {
					return null;
				}
			}

			public String toString() {
				return "element to be clickable: " + element;
			}
		};
	}
Hide result
+1
 begin
      @locator.click
      @locator.click
      return true
rescue
      return false
end
0
List<WebElement> wb=driver.findElements(By.xpath(newXpath));
        for(WebElement we: wb){
            if(we.isDisplayed() && we.isEnabled())
            {
                we.click();
                break;
            }
        }
    }
0

, , , .

  1. In my case, when I switch the button status, the css button color and cursor also change. To make sure the button is active, I used the following approach:
if (AddDocumentButton.Displayed & AddDocumentButton.GetCssValue("cursor").Equals("pointer") & AddDocumentButton.GetCssValue("color").Equals("rgb(22, 172, 0)"))
{
return true;
}
  1. I tried to click on the button and compare the number of tabs / windows in the browser.
-1
source

All Articles