Riidonesh
Riidonesh

Reputation: 63

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

I am trying an alternative way to select a menu item in the sidebar navigation. These elements are within an iframe. I have switched to the iframe and am trying to select an item from the side menu in order for the respective navigational items to be displayed for selection. These menus are sitting in a <ul> tag. You will notice that by default the very first menu/list item is selected. I am trying to select Customers. I am using Selenium webDriver for Java. See the screenshot with the HTML.

I have tried the following already, and other xpaths with no success:

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

This is one example of the error message that is returned:

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

I would get the same error using alternative xpath values.

enter image description here

Upvotes: 1

Views: 1900

Answers (3)

Riidonesh
Riidonesh

Reputation: 63

I have decided to follow the top menu navigation approach and it works as follows:

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

Upvotes: 1

Rakesh Singh Chouhan
Rakesh Singh Chouhan

Reputation: 233

As you have mentioned the element you are trying to access is inside an iFrame so make sure you switch to iFrame first.

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

Then make sure you wait for iFrame/ or wait for below element to load

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

Upvotes: 1

Swaroop Humane
Swaroop Humane

Reputation: 1836

Please use the below 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()

Run it and let me know if you get any errors.

Upvotes: 1

Related Questions