Selenium webcrawling组合框

时间:2012-09-11 21:02:14

标签: python selenium combobox

我试图在python中单击一个java脚本组合框,但是如果我正常执行它会给我一个错误,说组合框是隐藏的,我programitcally等待组合框出现,但它不会出现。组合框中的此选项是一个子菜单,但是,如果我从真实菜单中选择一个选项,它可以工作但不能使用子菜单选项。这是网站https://mbsdisclosure.fanniemae.com/PoolTalk2/index.html,>高级搜索> #then组合框我正在寻找Primary Mega的子菜单选项>初级Mega:Fannie Mae / Ginnie Mae支持可调整利率。谢谢!

1 个答案:

答案 0 :(得分:0)

Selenium将无法单击,因为您要选择的元素被视为不可见(不活动)。所以唯一的方法(imho)使用js来解决这个问题。 这在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());

    }

希望这可以帮助你)