繁体   English   中英

Selenium Java: 侧边栏菜单导航-> Unable Select 无序列表菜单项

[英]Selenium Java : Side bar menu navigation - > Unable Select the un-ordered list menu item

我正在尝试 select 侧边栏导航中的菜单项的替代方法。 这些元素在 iframe 中。我已切换到 iframe 并尝试从侧面菜单中选择 select 一个项目,以便显示相应的导航项目以供选择。 这些菜单位于<ul>标记中。 您会注意到默认情况下第一个菜单/列表项被选中。 我正在尝试 select 客户。 我正在为 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 值时,我会得到相同的错误。

在此处输入图像描述

请使用以下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()

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

正如您所提到的,您尝试访问的元素位于 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();

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM