I am trying to click a java script combo box in python, however If i do it normally, it gives me an error saying that the combo box is hidden, I programitcally wait for the combo box to appear but it does not appear. This option in combo box is a sub menu, however, if i select just an option from real menu it works but not with sub menu options. This is the website, https://mbsdisclosure.fanniemae.com/PoolTalk2/index.html, > Advanced Search > #then the combo box I am looking for the sub menu option for Preliminary Mega > Preliminary Mega: Fannie Mae/Ginnie Mae backed Adjustable Rate . Thanks!
Asked
Active
Viewed 410 times
1 Answers
0
Selenium won't be able to click as element you want to select is considered to be invisible(inactive). So the only way(imho) to use js to resolve this issue. That worked for me in java:
@Test
public void neeededDropdownSelect() throws InterruptedException {
driver.get("https://mbsdisclosure.fanniemae.com/PoolTalk2/index.html");
jsClickOnElement("li#tab_1>a>span");
WebElement dropdownMenu = fluentWait(By.cssSelector("span#asSelectedSecType"));
dropdownMenu.click();
jsClickOnElement("div[class=\"fg-menu-container ui-widget ui-widget-content ui-corner-all fg-menu-flyout\"] ul[class=\"fg-menu ui-corner-all\"]>li>a[id=\"MEGA_INTERIM\"]");
jsClickOnElement("div[class=\"fg-menu-container ui-widget ui-widget-content ui-corner-all fg-menu-flyout\"] ul[class=\"fg-menu ui-corner-all\"]>li>ul.ui-corner-all a[id=\"MEGA_INTERIM_ARM\"]");
}
public WebElement fluentWait(final By locator){
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(
new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(locator);
}
}
);
return foo; } ;
public void jsClickOnElement(String cssSel){
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\'"+cssSel+"\');");
stringBuilder.append("x.click();");
js.executeScript(stringBuilder.toString());
}
Hope this helps you)

eugene.polschikov
- 7,254
- 2
- 31
- 44
-
Anyway I could use this in python? – Sep 18 '12 at 14:46
-
this code I presented you in java, so I suppose you can simply adopt it to python because css selectors "div[class=\"fg-menu-container ui-widget ui-widget-content ui-corner-all fg-menu-flyout\"] ul[class=\"fg-menu ui-corner-all\"]>li>a[id=\"MEGA_INTERIM\"]"); jsClickOnElement("div[class=\"fg-menu-container ui-widget ui-widget-content ui-corner-all fg-menu-flyout\"] ul[class=\"fg-menu ui-corner-all\"]>li>ul.ui-corner-all a[id=\"MEGA_INTERIM_ARM\"]"); are correct and that piece of code worked for me (i checked it=) – eugene.polschikov Sep 18 '12 at 14:58
-
I get this error `selenium.common.exceptions.ElementNotVisibleException: Message: u'Element is not currently visible and so may not be interacted with' ` when i do `("div[class=\"fg-menu-container ui-widget ui-widget-content ui-corner-all fg-menu-flyout\"] ul[class=\"fg-menu ui-corner-all\"]>li>ul.ui-corner-all a[id=\"MEGA_INTERIM_ARM\"]")` – Sep 18 '12 at 15:13
-
do try then integrate js into your python code watch this link: http://stackoverflow.com/questions/683462/best-way-to-integrate-python-and-javascript then you'll be able to use my js code which always click on the invisible elements – eugene.polschikov Sep 18 '12 at 15:18
-
is there an equivilant to the js `jsClickOnElement` in python, i tried to use mouseover but am unable to set the duration of the mouseover. – Sep 18 '12 at 15:43
-
obvious solution: you simply shoulda find how it is possible to execute js code in python. LIke here it is described: http://stackoverflow.com/questions/2753878/how-to-evaluate-javascript-code-in-python I would like to provide python code in details, but unfortunately I work on java( – eugene.polschikov Sep 19 '12 at 09:51
-
I found pyv8, but how would i use your script with my script? – Sep 26 '12 at 14:40
-
ok. I'll investigate into pyv8 in the nearest future and will try to give you a piece of advice concerning adoptation. – eugene.polschikov Sep 26 '12 at 15:51