I am new & trying to learn selenium I am able to select a specific element from the list as I know the xpath:
driver.findElement(By.xpath("*[@id='category-dropdown']/descendant::*[@title='Events']"))
Can anyone please tell me how can I select any item from the list as each item will have a different title name? I just want to select an item by passing the title name as a variable eg select "Jobs" or "Cars & bikes"
I tried the below code but i am getting the list element size as 0
List<WebElement> element=driver.findElements(By.xpath(.//*[@id='category-dropdown']/ul/li));
System.out.println(element.size());
for (WebElement webElement : element)
{
System.out.println(webElement.getText());
}
the element size is zero
<div id="category-dropdown" class="drop-layers cate-layer" style="display: block;" data-type="overlay" data-action="focus" data-area="query">
<ul>
<li>
<a href="javascript:void(0);" data-sc_cid="1" data-sc_gid="0" data-sc_cn="all" data-sc_dn="All Categories" title="All Categories"> All Categories </a>
</li>
<li>
<a href="javascript:void(0);" data-sc_cid="1397" data-sc_gid="60" data-sc_cn="cars-bikes" data-sc_dn="Cars & Bikes" title="Cars & Bikes"> Cars & Bikes </a>
</li>
<li>
<a href="javascript:void(0);" data-sc_cid="18224025" data-sc_gid="269" data-sc_cn="mobiles-tablets" data-sc_dn="Mobiles & Tablets" title="Mobiles & Tablets"> Mobiles & Tablets </a>
</li>
<li>
<a href="javascript:void(0);" data-sc_cid="18222212654" data-sc_gid="247" data-sc_cn="electronics-appliances" data-sc_dn="Electronics & Appliances" title="Electronics & Appliances"> Electronics & Appliances </a>
</li>
<li>
<a href="javascript:void(0);" data-sc_cid="1405" data-sc_gid="20" data-sc_cn="real-estate" data-sc_dn="Real Estate" title="Real Estate"> Real Estate </a>
</li>
<li>
<a href="javascript:void(0);" data-sc_cid="1325" data-sc_gid="123" data-sc_cn="services" data-sc_dn="Services" title="Services"> Services </a>
Looking at your locator for the list
List<WebElement> element=driver.findElements(By.xpath(.//*[@id='category-dropdown']/ul/li));
You've not enclosed the xpath in quotations, so it won't recognise it as valid, it should be
List<WebElement> element=driver.findElements(By.xpath("//*[@id='category-dropdown']/ul/li"));
Whenever locate an element ""
is required.
For example:
driver.findElements(By.id("xyz"));
driver.findElements(By.className("xyz"));
driver.findElements(By.xpath("//*[@id='xyz']"));
simply change this statement
List<WebElement> element=driver.findElements(By.xpath("//*[@id='category-dropdown']/ul/li"));
rest is fine
You can use XPath for that. When you do not have to click on the dropdown first;
WebElement el = driver.findElement(By.xpath("//div[@id = 'category-dropdown']/descendant::li[text() = 'Cars @ Bikes']"));
el.click();
When you have to click the dropdown first:
WebElement dropdown = driver.findElement(By.id("category-dropdown"));
dropdown.click();
dropdown.findElement(By.cssSelector("li[value='Cars & Bikes']")).click();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.