Reputation: 682
I would like to click on a button, inside a list using selenium webdriver with java.Like in the image bellow. I tried with id of the li, but it doesn't work :
WebElement slick0 = driver.findElement(By.id("slick-slide00"));
wait.until(ExpectedConditions.visibilityOf(slick0));
slick0.click();
I tried with the xpath, that i got with firepath doesn't work too:
WebElement slick1 = driver.findElement(By.xpath(".//*[@id='slick-slide01']/button"));
System.out.println("click sign");
slick1.click();
And this is what I'm getting in console
Element info: {Using=xpath, value=.//*[@id='slick-slide01']/button}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:215)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:167)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:671)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:410)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:509)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:402)
at ChromeTests.FirstChrome.main(FirstChrome.java:32)
Can someone tell me which selector and from wher should I choose .
Thank you.
<ul class="slick-dots" style="display: block;" role="tablist">
<li id="slick-slide00" class="" aria-hidden="true" role="presentation" aria-selected="true" aria-controls="navigation00">
<li id="slick-slide01" class="slick-active" aria-hidden="false" role="presentation" aria-selected="false" aria-controls="navigation01">
<button type="button" data-role="none" role="button" aria-required="false" tabindex="0">2</button>
</li>
<li id="slick-slide02" aria-hidden="true" role="presentation" aria-selected="false" aria-controls="navigation02">
<li id="slick-slide03" aria-hidden="true" role="presentation" aria-selected="false" aria-controls="navigation03">
</ul>
Upvotes: 1
Views: 2861
Reputation: 21
You can get the list of elements, like this:
public static List<WebElement> listElements(){
return driver.findElements(By.xpath("//li[starts-with(@id, 'slick-slide')]"));
}
Then you can use a for loop to go through each of the elements from the array or use listElements().get(i)
where 'i' is any of the buttons you need from that list.
Note that I couldn't test it, so you might need to work a bit on the locator, but the theory should be correct (it works for me in many cases).
Upvotes: 1
Reputation: 37
I don't think it is a locator related issue, I faced a similar issue where locator was right but selenium was not able to perform click I used Javascript in that case and it worked ,Also one other reason that i can think of is element you want to be clicked is in a frame and locator you created is pointing to an element in default frame.
Upvotes: 1
Reputation: 1270
You can try below code, you might have to try couple of different things which I will try to cover. Since complete DOM and page is not available so practically, I have not tested below code.
Whole idea is will first try to get the outer div, which contains "ul" and "li" tag. Below is the code through which, I am trying to locate the outer parent div.
public WebElement getOuterDiv(){
return driver.findElement(By.xpath("//div[contains(@class, 'slick-list draggable')]"));
}
Now make sure the above div class name is correct, if the outer div you are able to locate then it's great otherwise try to go one level above for locating div class "slick-initiated slick-slider"
Once you have the outer div then try and locating the inner "ul" and "li" class. Below is the code for locating the inner element of the outer div class we have located.
public void getUlElement(){
getOuterDiv().findElement(By.className("slick-dots")).findElement(By.id("slick-slide01")).click();
}
Here is the explanation for the above code and what you need to do in case inner classname is not found.
getOuterDiv().findElement(By.className("slick-dots")) with the help of the outer class, I am trying to locate the inner element of the outer div class which is a . If the findElement(By.className("slick-dots")) works then go ahead otherwise try locating the inner with the tagname using findElement(By.tagName("ul")).
Finally click on the innermost "li" tag with the id. findElement(By.id("slick-slide01")).click();
Let me know if you need any help or any of the steps is not clear or does not works out.
Upvotes: 1