如何自动单击导航菜单以避免 Selenium Java 中的 NoSuchElementException?
How to automate the clicking of the navigation menu avoiding NoSuchElementException in Selenium Java?
我已经实现了一个 Selenium Java
程序,我需要在其中自动点击导航菜单。下面是我的代码的实现。
NewCard.java
public class NewCard {
public static void createTopUpRequestNewCard(WebDriver driver) {
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement utilitytopup = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='wrapper']/div/ul/li[9]/a")));
utilitytopup.click();
WebElement createtopupnewcard = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(.,'create top up request (new card)')]")));
createtopupnewcard.click();
}
}
实用充值菜单
<a href="#" class="menu__link"><svg aria-hidden="true" class="svg-inline--fa fa-user fa-w-14 menu__icon" focusable="false" data-prefix="fa" data-icon="user" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" data-fa-i2svg=""><path fill="currentColor" d="M224 256c70.7 0 128-57.3 128-128S294.7 0 224 0 96 57.3 96 128s57.3 128 128 128zm89.6 32h-16.7c-22.2 10.2-46.9 16-72.9 16s-50.6-5.8-72.9-16h-16.7C60.2 288 0 348.2 0 422.4V464c0 26.5 21.5 48 48 48h352c26.5 0 48-21.5 48-48v-41.6c0-74.2-60.2-134.4-134.4-134.4z"></path></svg><!-- <i aria-hidden="true" class="fa fa-user menu__icon"></i> --> Utility Top Up <svg aria-hidden="true" class="svg-inline--fa fa-chevron-right fa-w-10 menu__arrow-icon" focusable="false" data-prefix="fa" data-icon="chevron-right" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512" data-fa-i2svg=""><path fill="currentColor" d="M285.476 272.971L91.132 467.314c-9.373 9.373-24.569 9.373-33.941 0l-22.667-22.667c-9.357-9.357-9.375-24.522-.04-33.901L188.505 256 34.484 101.255c-9.335-9.379-9.317-24.544.04-33.901l22.667-22.667c9.373-9.373 24.569-9.373 33.941 0L285.475 239.03c9.373 9.372 9.373 24.568.001 33.941z"></path></svg><!-- <i aria-hidden="true" class="fa fa-chevron-right menu__arrow-icon"></i> --></a>
截图1
创建充值新卡(子部分)
<a href="#" class="context-menu__link"> create top up request (new card) </a>
截图2
我尝试输入 CssSelector
、Linktext
和 XPath
但无济于事。最重要的是,我使用 Selenium IDE
来记录它,上面的屏幕截图是我分别处理的 2 个元素。尽管如此,尽管尝试了这些定位器,我仍然得到 NoSuchElementException
.
我可以知道如何解决这个问题吗?
ExpectedConditions of doesn't ensures that the element is interactable. Instead you need to induce for the element_to_be_clickable()
and you can use the following :
public class NewCard {
public static void createTopUpRequestNewCard(WebDriver driver) {
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='menu__link' and contains(., 'Utility Top Up')]"))).click();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='menu__link' and contains(., 'create top up request')]"))).click();
}
}
我已经实现了一个 Selenium Java
程序,我需要在其中自动点击导航菜单。下面是我的代码的实现。
NewCard.java
public class NewCard {
public static void createTopUpRequestNewCard(WebDriver driver) {
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement utilitytopup = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='wrapper']/div/ul/li[9]/a")));
utilitytopup.click();
WebElement createtopupnewcard = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(.,'create top up request (new card)')]")));
createtopupnewcard.click();
}
}
实用充值菜单
<a href="#" class="menu__link"><svg aria-hidden="true" class="svg-inline--fa fa-user fa-w-14 menu__icon" focusable="false" data-prefix="fa" data-icon="user" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" data-fa-i2svg=""><path fill="currentColor" d="M224 256c70.7 0 128-57.3 128-128S294.7 0 224 0 96 57.3 96 128s57.3 128 128 128zm89.6 32h-16.7c-22.2 10.2-46.9 16-72.9 16s-50.6-5.8-72.9-16h-16.7C60.2 288 0 348.2 0 422.4V464c0 26.5 21.5 48 48 48h352c26.5 0 48-21.5 48-48v-41.6c0-74.2-60.2-134.4-134.4-134.4z"></path></svg><!-- <i aria-hidden="true" class="fa fa-user menu__icon"></i> --> Utility Top Up <svg aria-hidden="true" class="svg-inline--fa fa-chevron-right fa-w-10 menu__arrow-icon" focusable="false" data-prefix="fa" data-icon="chevron-right" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512" data-fa-i2svg=""><path fill="currentColor" d="M285.476 272.971L91.132 467.314c-9.373 9.373-24.569 9.373-33.941 0l-22.667-22.667c-9.357-9.357-9.375-24.522-.04-33.901L188.505 256 34.484 101.255c-9.335-9.379-9.317-24.544.04-33.901l22.667-22.667c9.373-9.373 24.569-9.373 33.941 0L285.475 239.03c9.373 9.372 9.373 24.568.001 33.941z"></path></svg><!-- <i aria-hidden="true" class="fa fa-chevron-right menu__arrow-icon"></i> --></a>
截图1
创建充值新卡(子部分)
<a href="#" class="context-menu__link"> create top up request (new card) </a>
截图2
我尝试输入 CssSelector
、Linktext
和 XPath
但无济于事。最重要的是,我使用 Selenium IDE
来记录它,上面的屏幕截图是我分别处理的 2 个元素。尽管如此,尽管尝试了这些定位器,我仍然得到 NoSuchElementException
.
我可以知道如何解决这个问题吗?
ExpectedConditions of element_to_be_clickable()
and you can use the following
public class NewCard {
public static void createTopUpRequestNewCard(WebDriver driver) {
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='menu__link' and contains(., 'Utility Top Up')]"))).click();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='menu__link' and contains(., 'create top up request')]"))).click();
}
}