streets36
streets36

Reputation: 13

How to select a specific list item from an unordered list in Selenium Web Driver

I am trying to select a specific list item using Selenium Web driver from an UL, but when the test runs it doesn't find locate the Element.

My Selenium Code:

driver.findElement(By.id("postcode-field")).click();
driver.findElement(By.id("postcode-field")).sendKeys("GL32BN");
driver.findElement(By.id("postcode-field")).submit();
driver.findElement(By.id("5000955")).click();

The related HTML:

<ul class="address-list">
<li data-uprn="" data-premise-id="5127035" data-site-id="05">
            1 Pelham Crescent, Gloucester, GL3 2BN
        </li>
<li data-uprn="" data-premise-id="5000955" data-site-id="05">
            2 Pelham Crescent, Gloucester, GL3 2BN
        </li>

Upvotes: 0

Views: 1569

Answers (2)

JeffC
JeffC

Reputation: 25620

This code worked fine for me. I'm not sure if you tried a wait or not.

// enter the postcode into the field
driver.findElement(By.id("postcode-field")).sendKeys("GL32BN");
// click the magnifying glass icon to initiate the search
driver.findElement(By.cssSelector("button[data-text-original='Submit']")).click();
// wait for the desired element to be clickable (it takes a fraction of a second to open the dropdown and populate it)
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("li[data-premise-id='5127035']")))
        .click();
// click the next button
driver.findElement(By.id("next")).click();

Upvotes: 1

Harinder
Harinder

Reputation: 39

5000955 is not the id attribute's value which you have used in your code. 5000955 is the value of 'data-premise-id' attribute.
you can use driver.findElement(By.xpath("//*[data-premise-id='5000955']")).click(); instead of driver.findElement(By.id("5000955")).click();

To select list item, first of all You have to click on an element, from where list is display, then, click on list item.

and make sure that data-premise-id attribute's value should be static.

Upvotes: 0

Related Questions