我正在公共网站上工作:
http://preview.harriscountyfws.org/
下面显示的逻辑遇到ElementNotVisibleException。 但是,如果您取消注释以“//”开头的两行,并注释掉该代码上方的两行,则代码工作正常(但这适用于不同于我想要的下拉列表。)
所以我很困惑。 我可以从“最后一次降雨”下拉菜单中选择就好了。 但是使用完全相同的方法,我无法从“Gage by Location”下拉列表中选择。
如何从Gage by Location位置下拉菜单中进行选择?
package sdfwe.sdwd.qsde;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Gfgsdrfffde {
public static void main(String[] args) {
System.out.println("Running Tests....");
// Optional, if not specified, WebDriver will search your path for chromedriver.
System.setProperty("webdriver.chrome.driver", "C:\\ChromeDriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://preview.harriscountyfws.org/");
driver.manage().window().maximize();
WebElement we;
String searchText;
WebElement dropdown;
//if you comment out the next two lines after these comments and
// uncomment out the commented lines below, the code works
searchText = "110:A100_110 Clear Creek @45";
dropdown = driver.findElements(By.cssSelector("span.k-widget")).get(3);
// searchText = "24 Hours";
// dropdown = driver.findElements(By.cssSelector("span.k-widget")).get(1);
System.out.println("Attempting Dropdown Click....");
dropdown.click();
System.out.println("Finished running Dropdown Click....");
Sleep(4);
//Select dd2 = new Select(driver.findElement(By.id("ddlTimeSpan")));
//dd2.selectByVisibleText(searchText);
we = driver.findElement(By.xpath("//li[text()='" + searchText + "']"));
if (we != null) {
we.click();
}
System.out.println("Finished Selecting Dropdown....");
//driver.quit();
}
public static void Sleep(int i)
{
try { Thread.sleep(i * 300); }
catch(InterruptedException ie) { System.out.println("Unexpected error in sleep"); }
}
}
答案 0 :(得分:0)
你可以试试这个:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("select#ddlGage")));
Select gageByLocationSelect = new Select(driver.findElement(By.cssSelector("select#ddlGage")));
gageByLocationSelect.selectByVisibleText("110:A100_110 Clear Creek @ I-45");
希望这会对你有所帮助!!
答案 1 :(得分:0)
我尝试了您的代码,您需要对其进行这些更改才能正常工作:
searchText = "110:A100_110 Clear Creek @ I-45";
dropdown = driver.findElement(By.cssSelector("span[aria-owns='ddlGage_listbox']"));
以下是基于上述示例的完整工作代码。
public static void main(final String[] args) {
System.out.println("Running Tests....");
// Optional, if not specified, WebDriver will search your path for
// chromedriver.
System.setProperty("webdriver.chrome.driver", "C:\\ChromeDriver\\chromedriver.exe");
final WebDriver driver = new ChromeDriver();
driver.get("http://preview.harriscountyfws.org/");
driver.manage().window().maximize();
WebElement we;
String searchText;
WebElement dropdown;
// if you comment out the next two lines after these comments and
// uncomment out the commented lines below, the code works
searchText = "110:A100_110 Clear Creek @ I-45";
dropdown = driver.findElement(By.cssSelector("span[aria-owns='ddlGage_listbox']"));
// searchText = "24 Hours";
// dropdown =
// driver.findElements(By.cssSelector("span.k-widget")).get(1);
System.out.println("Attempting Dropdown Click....");
dropdown.click();
System.out.println("Finished running Dropdown Click....");
Sleep(4);
// Select dd2 = new Select(driver.findElement(By.id("ddlTimeSpan")));
// dd2.selectByVisibleText(searchText);
we = driver.findElement(By.xpath("//li[text()='" + searchText + "']"));
if (we != null) {
we.click();
}
System.out.println("Finished Selecting Dropdown....");
// driver.quit();
}
此外,对于简单的selenium脚本,您可能需要检查https://github.com/codezombies/easytest。
这是使用easytest框架的相同脚本。
public static void main(String[] args) throws Exception {
try(EasyTest easy = new EasyTest(DriverType.CHROME)) {
easy.start("http://preview.harriscountyfws.org/");
easy.newPage(page -> {
page.click("span[aria-owns='ddlGage_listbox']");
final String searchText = "110:A100_110 Clear Creek @ I-45";
page.executeIn("#ddlGage_listbox", container -> {
page.click("//li[text()='" + searchText + "']");
});
});
}
}