Selenium webcrawling组合框 [英] Selenium webcrawling combo box

查看:147
本文介绍了Selenium webcrawling组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在python中点击一个java脚本组合框,但如果我这样做,它给我一个错误,说组合框是隐藏的,我programitcally等待组合框出现,但它不出现。组合框中的此选项是一个子菜单,但是,如果我从实际菜单中选择一个选项,它工作,但不与子菜单选项。这是网站, https://mbsdisclosure.fanniemae.com/PoolTalk2/index.html,>高级搜索> #then组合框我正在寻找子菜单选项初步巨型>初步巨型:联邦抵押协会/ Ginnie Mae支持可调整率。谢谢!

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!

推荐答案

Selenium无法点击您想要选择的元素被视为不可见(非活动)。所以唯一的方法(imho)使用js来解决这个问题。
这对我在java工作:

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());

    }

希望这有助于你)

这篇关于Selenium webcrawling组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆