I'm new to Selenium
and need to check if element is clickable in Selenium
Java
, since element.click()
passes both on link
and label
.
I tried using below code but not working:
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)
Need a help on this.
elementToBeClickable
is used for checking an element is visible and enabled such that you can click it.ExpectedConditions.elementToBeClickable
returnsWebElement
if expected condition is true otherwise it will throwTimeoutException
, It never returnsnull
.So if your using
ExpectedConditions.elementToBeClickable
to find an element which will always gives you the clickable element, so no need to check fornull
condition, you should try as below :-As you are saying
element.click()
passes both onlink
andlabel
that's doesn't mean element is not clickable, it means returned elementclicked
but may be there is no event performs on element by click action.Note:- I'm suggesting you always try first to find elements by
id
,name
,className
and other locator. if you faced some difficulty to find then usecssSelector
and always give last priority toxpath
locator because it is slower than other locator to locate an element.Hope it helps you..:)
wait.until(ExpectedConditions)
won't return null, it will either meet the condition or throwTimeoutException
.You can check if the element is displayed and enabled
From the source code you will be able to view that,
ExpectedConditions.elementToBeClickable()
, it will judge the element visible and enabled, so you can useisEnabled()
together withisDisplayed()
. Following is the source code.There are instances when
element.isDisplayed() && element.isEnabled()
will returntrue
but still element will not be clickable, because it is hidden/overlapped by some other element. In such case,Exception
caught is:It will work.