之前已审核过此问题的帖子,但问题仍然存在。 http://preview.harriscountyfws.org/是一个与此问题相关的公共网站。
我正在尝试点击下拉菜单,然后从降雨量下拉菜单中选择“渠道状态”。
我收到以下错误:
线程“main”中的异常org.openqa.selenium.ElementNotVisibleException:元素不可见:元素当前不可见,可能无法操作
我附上了代码截图,但您也可以访问该网站并按F12
查看代码。
以下是我目前基于迄今为止所做研究的代码:
Select dropdown = new Select(driver.findElement(By.id("siteType")));
WebElement triggerDropDown = driver.findElement(By.className("k-i-arrow-s"));
triggerDropDown.click();
dropdown.selectByVisibleText("Channel Status");
dropdown.selectByIndex(1);
显示的最后两个代码语句都没有工作(dropdown.select ...)
两者都会产生ElementNotVisibleException
。
嗯,这不是真的,因为按下triggerDropDown.Click()
,选项是可见的!
答案 0 :(得分:1)
使用以下代码:
driver.get("http://preview.harriscountyfws.org/");
driver.manage().window().maximize();
Thread.sleep(2000);//use wait using until instead of this wait
WebElement elem = driver.findElement(By.xpath("//span[text() = 'Rainfall']"));
elem.click();
Thread.sleep(2000);
for(int i = 0; i <= 2; i++){//2 is used bacause u have 2 options
Actions actions = new Actions(driver);
actions.sendKeys(Keys.DOWN).build().perform();//press down arrow key
Actions actions2 = new Actions(driver);
actions2.sendKeys(Keys.ENTER).build().perform();//press enter
}
这将点击频道状态按钮。
答案 1 :(得分:0)
这是一个奇怪的。我可以轻松点击下拉菜单,但点击&#34;频道状态&#34;没有用。关于该下拉列表的某些内容并未表现为正常&#34;。我尝试过典型的WebDriverWait
,但它不起作用。 Selenium没有正确等待或正在进行其他事情。我很少推荐Thread.sleep()
,但在这种情况下,我无法找到解决方法。
以下代码有效。
String searchText = "Channel Status";
driver.findElement(By.cssSelector("span.k-widget.k-dropdown.k-header")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//li[text()='" + searchText + "']")).click();