【问题标题】:Selenium Java : Side bar menu navigation - > Unable Select the un-ordered list menu itemSelenium Java:侧栏菜单导航->无法选择无序列表菜单项
【发布时间】:2020-08-17 21:32:43
【问题描述】:

我正在尝试另一种方法来选择侧边栏导航中的菜单项。这些元素位于 iframe 中。我已切换到 iframe 并尝试从侧面菜单中选择一个项目,以便显示相应的导航项目以供选择。这些菜单位于<ul> 标签中。您会注意到默认情况下第一个菜单/列表项被选中。我正在尝试选择客户。我正在为 Java 使用 Selenium webDriver。查看带有 HTML 的屏幕截图。

我已经尝试过以下方法,其他xpaths 没有成功:

  1. Xpath-//*[@class='crm_sitemap_catalog_item' and contains(text(),'Customers')]
  2. Xpath-//*[@id="catalog"]/ul/li[5]/div[2]/div
  3. 完整的 Xpath-//html/body/div[1]/div[1]/ul/li[5]/div[2]/div
  4. CSS 选择器-#catalog > ul > li.crm_sitemap_catalog_item.selected > div.body1 > div

这是返回的错误消息的一个示例:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@class='crm_sitemap_catalog_item' and contains(text(),'Customers')]"}

如果使用其他 xpath 值,我会得到同样的错误。

【问题讨论】:

    标签: java html selenium


    【解决方案1】:

    请使用下面的XPath

    wait = WebDriverWait(driver, 20)
    LiOption = wait.until(EC.presence_of_element_located((By.XPATH, "//div[text()='Customers']/parent::div/parent::li")))
    action.move_to_element(LiOption).click(LiOption).perform()
    

    运行它,如果您遇到任何错误,请告诉我。

    【讨论】:

    • 好的,我马上试试这个
    • 不幸的是,这个 XPath 值不起作用,它还返回 NoSuchElementException。感谢您尝试提供帮助
    • 我应该用实际元素的名称替换父值吗? >parent::div/parent::li'
    • 不,它应该按原样工作。可以分享网址吗?
    • wait = WebDriverWait(driver, 20) LiOption = wait.until(EC.presence_of_element_located((By.XPATH, "//div[text()='Customers']/parent::div/parent::li"))) action.move_to_element(LiOption).click(LiOption).perform()
    【解决方案2】:

    正如您所提到的,您尝试访问的元素位于 iFrame 内,因此请确保先切换到 iFrame。

    driver.switchTo().frame() //frame() is overloaded method so you can use which suits you.
    

    然后确保等待 iFrame/ 或等待下面的元素加载

    driver.findElement(By.cssSelector(div[title='Customers'])).click();
    

    【讨论】:

    • 谢谢@Rakesh,我已切换到 iFrame,但我无法找到此元素。我还没有解决这个问题
    【解决方案3】:

    我决定采用顶部菜单导航方法,它的工作原理如下:

     public void PerformItemNavigation(WebElement onWebElement , WebDriver mydriver) {
        Actions objectAction = new Actions(mydriver);
        Capabilities cap = ((RemoteWebDriver) mydriver).getCapabilities();
        String browserName = cap.getBrowserName().toLowerCase();
    
        switch (browserName){
            case "chrome":
                    objectAction.moveToElement(onWebElement).perform();
                    objectAction.moveToElement(onWebElement).click().perform();
                break;
            case "firefox":
                ((RemoteWebDriver) 
                mydriver).executeScript("arguments[0].scrollIntoView();", onWebElement);
                objectAction.moveToElement(onWebElement).build().perform();
                objectAction.click();
                objectAction.perform();
                    break;
               
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 2018-09-28
      • 2017-10-07
      • 1970-01-01
      • 1970-01-01
      • 2014-07-03
      相关资源
      最近更新 更多