1

I have this menu that is Javascript Generated. I already tried to locate this menu using xpath but there is an error 'NoSuchElemetFound'. My goal here is to press the menu generated by javascript or Execute the command of the menu (like you click the menu for real).

The menu is in a div tag that is hidden.

Here is the command called by this javascript menu:

parent.navFrame.gotoURL('url');

Here are my current codes that does not work:

WebElement menu = driver.findElement(By.xpath("html/body/span/div[11]/div/div"));
WebElement parentMenu = driver.findElement(By.xpath("html/body/table/tbody/tr/td[6]/a/img"));
Actions builder = new Actions(driver);
builder.moveToElement(parentMenu).moveToElement(menu).click().build().perform();

and this

        Actions builder = new Actions(driver);
        ((HasInputDevices) driver).getMouse();
        builder.moveToElement(driver.findElement(By.xpath("html/body/table/tbody/tr/td[6]/a/img"))).build().perform();
        driver.findElement(By.xpath("html/body/table/tbody/tr/td[6]/a/img")).isSelected();
        Thread.sleep(1000L);    
        builder.moveToElement(driver.findElement(By.xpath(".//*[@id='menuItem101']"))).build().perform();
        driver.findElement(By.xpath(".//*[@id='menuItem101']")).click();
        Thread.sleep(1000L);

Please Help me. Thanks

3 Answers 3

3

I've seen that webdriver can't type into hidden fields, so it could be the case that you can't click hidden elements either.

If that's the case a potential workaround is to execute javascript

((IJavaScriptExecutor)driver).ExecuteScript("$('#theDivInQuestion').click()");

The example above requires JQuery, but it can be converted to regular java script if JQuery isn't available on your page

Sign up to request clarification or add additional context in comments.

Comments

0

The menu is in a div tag that is hidden.

If the element is hidden, then WebDriver can't click it.

2 Comments

is there any solution for this?
@hybrid_18 Activate what you need to do to make it visible, or execute a piece of JavaScript to do it.
0

Even if the menu is hidden, you can click it by the following snippet.

WebElement we = driver.findElement(By.xpath(xpathtotheELEMENT));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", we); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.