[英]Selenium IDE locator doesn't work in Selenium 3.3.1 Java
我有一個網頁,其中包含2個具有相同類名但具有不同div類的鏈接。 第一個是不可見的(在下拉菜單中),而我想要的另一個是可見的。 因此,我正在嘗試查找可見元素。
他的HTML:
<div class="mainActionPanel">
<a css="create"></a>
</div>
鏈接具有動態ID。 當我使用ID進行XPath搜索時,我可以正確找到該元素,但是由於每個頁面上的按鈕具有不同的ID,因此已棄用該元素。
我嘗試使用Selenium IDE定位元素,以下定位器起作用: css=div.mainActionPanel > a.create
問題出在我上面顯示的定位器上。 當我嘗試查找元素時,總是會遇到以下異常:
NoSuchElementException:元素By.cssSelector:css = div.mainActionPanel> a.create(第一個)(LazyElement)不存在。
他沒有找到。 我嘗試了幾種語法,例如FluentLenium文檔中的示例( $("form > input[data-custom=selenium]"
)),但是它不起作用。
此外, el(".create").click()
會引發ElementNotVisibleException,因為他選擇了下拉鏈接。
我如何找到合適的元素?
也許這應該對您有幫助。
// this will provide you list of web elements based on class name
List<WebElement> webElements = driver.findElements(By.className("mainActionPanel"));
然后,您必須通過遍歷列表來找出所需的元素,如下所示。
WebElement tempElement = null;
for (WebElement element : webElements) {
if (element.getAttribute("css").equals("create")) {
tempElement = element;
}
}
完整代碼如下,
List<WebElement> webElements = driver.findElements(By.className("mainActionPanel"));
WebElement tempElement = null;
for (WebElement element : webElements) {
if (element.getAttribute("css").equals("create")) {
tempElement = element;
}
}
//then you can perform that you want
tempElement.click();
使用以下命令從CSS搜索更改為xpath:
xpath = (//a[contains(@href, '#')])[5]
因為它是帶有href屬性的第四個“ a”元素,其中包含文本“#”。 不要找麻煩。
嘗試在下面使用:
WebElement elem = driver.findElement(By.cssSelector("div.mainActionPanel > a"));
WebDriverWait wait= new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOf(elem));
elem.click();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.