【问题标题】:How to automate the clicking of the navigation menu avoiding NoSuchElementException in Selenium Java?如何在 Selenium Java 中自动单击导航菜单以避免 NoSuchElementException?
【发布时间】:2021-04-22 20:02:02
【问题描述】:

我已经实现了一个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

我尝试输入CssSelectorLinktextXPath,但无济于事。最重要的是,我使用了Selenium IDE 来记录它,上面的屏幕截图是我正在处理的两个元素。尽管如此,尽管尝试了这些定位器,我仍然收到NoSuchElementException

我可以知道如何解决这个问题吗?

【问题讨论】:

  • 当点击发生时,是否有可能元素尚未完全加载?
  • 我已经尝试过显式等待。但无济于事
  • 有没有办法解决这个问题?
  • @Shotokan 可能是它的原因。我尝试过等待,但也无济于事
  • 你能尝试给HTML元素ID,然后尝试使用getElementByID()的方法吗?只是看看 XPath 是否有问题。

标签: java selenium-webdriver xpath css-selectors webdriverwait


【解决方案1】:

ExpectedConditions of visibilityOfElementLocated() 不能确保元素是可交互的。相反,您需要为element_to_be_clickable() 诱导WebDriverWait,您可以使用以下Locator Strategies

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();
    }
} 

【讨论】:

  • 我真的不知道该怎么办
猜你喜欢
  • 1970-01-01
  • 2019-03-26
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-03
相关资源
最近更新 更多