1

在我的搜索页面中,我有一个带有输入框和“开始”按钮的列表。我在控制台中获取列表值,但我想要的是迭代列表并在输入框中输入文本(文本是静态的)。每次选择列表时,它都会自动搜索,而不必按“开始”按钮。

WebElement search_list = driver.findElement(By.xpath("//*[@id='tl_tPlace']"));   
List <WebElement> li = search_list.findElements(By.tagName("option"));  
for(int i =0; i<li.size();i++){     
System.out.println(li.get(i).click());    
try{    
driver.findElement(By.xpath("//*@id='cmain_Place']")).sendKeys("text");

现在列表第一次具有“选择状态”,它通过第二个循环并选择列表项。选择后,页面会重新加载。然后有一个错误信息:

Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
4

1 回答 1

0

这完全是预期的行为,因为 DOM 在页面时完全重新创建,因此您对元素的任何引用在页面刷新后都将变为无效。

于 2012-07-10T14:19:09.310 回答