Chetan_K
Chetan_K

Reputation: 57

Unable to click on a sub value of a tab, getting 'element not visible' exception in Selenium

Trying to click on the 'E-Commerce Demo Site' in the 'DEMO SITES' tab in 'http://toolsqa.com/' website using the below mentioned ways. But unable to click on it. Kindly help me on this issue.

Code1:

 WebDriverWait wait = new WebDriverWait(driver, 10);
  wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.linkText("E-Commerce Demo Site"))));
  driver.findElement(By.linkText("E-Commerce Demo Site")).click();

Code2:

driver.findElement(By.cssSelector("#main-nav > li.menu-item.menu-item-type-custom.menu-item-object-custom.menu-item-has-children.menu-item-17611.dt-mega-menu.mega-auto-width.mega-column-1.has-children > ul > li.menu-item.menu-item-type-custom.menu-item-object-custom.menu-item-21575.dt-mega-parent.wf-1.first.level-arrows-on")).click();

Code3:

WebElement subNav = driver.findElement(By.className("sub-nav"));
List < WebElement > subNavValues = subNav.findElements(By.tagName("li"));

subNavValues.get(0).click();

Upvotes: 2

Views: 838

Answers (5)

Rajkumar
Rajkumar

Reputation: 58

     Actions ac;
 ac = new Actions(dr);
ac.MoveToElement(dr.FindElement(By.LinkText("DEMOSITES"))).Perform();

Then use any of the following xpaths to click on the submenu:

1."//span[contains(text(),'E-Commerce Demo Site')]"

2."//a[@href='http://store.demoqa.com']"

dr.findelement(By.xpath(xpath)).click();

Upvotes: 2

Anton Savostytskyi
Anton Savostytskyi

Reputation: 178

Try something like this it works fine for me:

@Test
public void qaToolsNavigation() {
    Actions action = new Actions(driver);
    By demoSitesMenuItem = By.xpath("//span[text()='DEMO SITES']");
    By eCommerceSubMenuItem = By.xpath("(//span[text()='E-Commerce Demo Site']/..)[1]");
    driver.get("http://toolsqa.com/");
    waitForVisibility(demoSitesMenuItem, 10);
    action.moveToElement(driver.findElement(demoSitesMenuItem)).build().perform();
    waitForVisibility(eCommerceSubMenuItem, 10);
    driver.findElement(eCommerceSubMenuItem).click();
}

private void waitForVisibility(By locator, int timeToWait) {
    WebDriverWait wait = new WebDriverWait(driver, timeToWait);
    wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
}

Upvotes: 1

Ab123
Ab123

Reputation: 423

After Maximizing of your window try below things

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement elem = driver.findElement(By.xpath("//nav[@id='navigation']/ul/li[9]/a/span"));
Actions action = new Actions(driver);
action.moveToElement(elem).build().perform();

This is working for me.

Upvotes: 1

Grasshopper
Grasshopper

Reputation: 9058

  1. You need to move to the "Demo sites" span. You have to use the Actions class to move to this element using the function moveToElement(). For the element search for the span with the text "DEMO SITES".

  2. Wait for the sub-menu to be displayed. Use webdriver wait on the element ul with class='sub-nav'. new WebDriverWait(driver, 3).until(ExpectedConditions.visibilityOf(sub-menu webelement)).

  3. Then you click on the 'E-Commerce Demo Site' link. Which is in the span with the text.

You can figure out the exact syntax.

Upvotes: 1

Dingo
Dingo

Reputation: 31

You can Try This: driver.findElement(By.xpath("//*//span[contains(text(),'E-Commerce Demo Site')]")).click();

Upvotes: 0

Related Questions