我正在尝试使用来自UL的Selenium Web驱动程序选择一个特定的列表项,但是当测试运行时,它找不到找到元素。
我的硒代码:
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();
相关的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>
答案 0 :(得分:-1)
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.
答案 1 :(得分:-1)
这段代码对我来说很好。我不确定你是否尝试过等待。
// 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();