Domanda

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

È stato utile?

Soluzione

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

Altri suggerimenti

The menu is in a div tag that is hidden.

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

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); 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top