我需要单击列表中的第一个元素。
我还尝试在单击之前对元素进行类型转换,但它也引发了异常。
wait = new WebDriverWait(driver,60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(ClientUICommon.getClientUIPaths().getProperty("BugFRList"))));
System.out.println("Bug to be clicked ");
ClientUICommon.sleep(3000);
((WebElement) driver.findElements(By.cssSelector(ClientUICommon.getClientUIPaths().getProperty("BugFRList")))).click();
最佳答案
问题出在下面的代码中:
((WebElement) driver.findElements(By.cssSelector(ClientUICommon.getClientUIPaths().getProperty("BugFRList")))).click();
你看,方法findElements
不返回WebElement
目的。它返回 WebElements 列表。 List<WebElement>
准确地说。
您基本上所做的是单击列表,而不是它的元素:
driver.findElements(...).click();
这会抛出编译错误,因为 List<>
没有方法click()
。为了单击列表的第一个元素,您应该使用 get
带有索引参数的方法如下:
list.get(0);
以上将返回单WebElement
完整代码:
List<WebElement> elementList = driver.findElements(By.cssSelector(ClientUICommon.getClientUIPaths().getProperty("BugFRList"))));
WebElement firstElement = elementList.get(0);
firstElement.click();
关于java - 我试图单击列表中的第一个元素,但编译器每次都会抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56983726/