【问题标题】:Selenium - How to check and increment dropdown value if it is already present?Selenium - 如果已经存在,如何检查和增加下拉值?
【发布时间】:2015-10-21 21:47:15
【问题描述】:

我正在尝试使用 Selenium Webdriver (Java) 自动化应用程序。我的 Web 应用程序有一个添加按钮。单击添加按钮后,将启用一个下拉菜单。如果再次单击它,将启用第二个下拉菜单。每个后续下拉列表的 ID 将是 page1、page2、page3.. 等等..

我想要做的是当我打开页面时,我需要找出页面中是否已经存在任何下拉菜单,如果是,则选择下一个下拉值,然后从下拉列表中选择一个值。

这是我当前的代码,我在其中手动选择每个下拉菜单并选择它们各自的值。:

driver.findElement(By.id("addPage")).click();
new Select(driver.findElement(By.id("page0"))).selectByVisibleText("ABCD");
driver.findElement(By.id("addPage")).click();
Thread.sleep(1000);
new Select(driver.findElement(By.id("page1"))).selectByVisibleText("CDEF");
driver.findElement(By.id("addPage")).click();
Thread.sleep(1000);
new Select(driver.findElement(By.id("page2"))).selectByVisibleText("EFGH");
driver.findElement(By.id("addContact")).click();

【问题讨论】:

    标签: javascript java selenium selenium-webdriver automation


    【解决方案1】:

    我认为您可以找到以page 开头的id 的任何选择元素,获取id 属性值并单击下一页的下拉菜单。示例实现:

    WebElement existingPage = driver.findElement(By.cssSelector("select[id^=page]"));
    
    String nextPageID = Integer.toString(Integer.parseInt(existingPage.getAttribute("id").replaceAll("\\D+", "")) + 1);
    Select nextPage = new Select(driver.findElement(By.id("page" + nextPageID)));
    

    并且,正如@Iridann 正确指出的那样,要检查是否存在,请捕获NoSuchElementException 异常:

    try {
        WebElement existingPage = driver.findElement(By.cssSelector("select[id^=page]"));
        // ...
    } catch (NoSuchElementException e) {
        // no pages found
    }
    

    【讨论】:

    • 谢谢。您的代码几乎有所帮助。但我还不能使用 nextPage 值来选择下拉值。我正在尝试使用此代码existpage = driver.findElement(By.cssSelector("select[id^=page]")); String nextPageID = Integer.toString(Integer.parseInt(existpage.getAttribute("id")) + 1); Select nextPage = new Select(driver.findElement(By.id("page" + nextPageID))); new Select(driver.findElement(By.id("nextPage"))).selectByVisibleText("ASJD"); 这也不起作用:nextPage.selectByVisibleText("ASJD"); 我应该如何在那里使用 nextPage 变量?
    • @RahulBaskaran nextPage 是一个选择实例,您应该能够以这种方式使用它nextPage.selectByVisibleText("ASJD");。有什么症状,有什么错误吗?谢谢!
    • 到达此行后出现以下错误Select nextPage = new Select(driver.findElement(By.id("page" + nextPageID))); 错误Exception in thread "main" java.lang.NumberFormatException: For input string: "page3" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at automationFramework.AutoBPS.main(AutoBPS.java:43)
    • 这些是Select 声明之后的行:driver.findElement(By.id("addPage")).click(); nextPage.selectByVisibleText("Benefit Finance -> Payments");
    【解决方案2】:

    我会尝试按照以下方式做一些事情,假设页面中没有其他下拉元素(我从你的问题中假设是这种情况)。

    try {
    driver.findElement(By.tagName("select"))
    } catch (NoSuchElementException e) {
    //create first dropdown
    }
    

    您可以尝试使用页面上每个选择元素的 id 填充一个数组,并搜索与模式“page\d”匹配的元素的存在,然后从那里开始。

    【讨论】:

    • 实际上页面中还有一些下拉菜单。尽管所有这些都与此功能无关。而且我不想记录任何信息。如果前一个已经存在,我需要在下一个下拉列表中自动选择值。
    • 此功能的下拉菜单是否具有可用于检查其存在的命名约定?有什么可以让您在应用程序/页面中编写此类下拉列表独有的 xpath?
    • isDisplayed() 这里实际上不需要调用,如果找不到元素,NoSuchElementException 会被findElement 抛出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    • 2021-09-29
    相关资源
    最近更新 更多