由于javascript

时间:2018-09-20 11:42:41

标签: javascript selenium selenium-webdriver webdriver automated-tests

我有一个公共url,我在这里尝试测试每个区域中的区域(东部,西部等)和州(国家/地区)列表。区域下拉列表驱动状态下拉列表。两个选择下拉菜单均具有javascript。我编写了以下代码,但是该代码为我提供了每个后续区域的第一个区域的状态。无法获得区域的正确状态。

WebElement zoneDropDownElement = driver.findElement(By.xpath("//select[@id='ddlDealerZone'] | //select[@name='ddlDealerZone']"));
    Select zoneDropDown = new Select(zoneDropDownElement);

    //Get all options
    List<WebElement> zdd = zoneDropDown.getOptions();

    for (int j = 0; j < zdd.size(); j++) {
        System.out.println(zdd.get(j).getText());
        //zoneDropDown.selectByIndex(j);

        zoneDropDown.getOptions().get(j).click();
        jse.executeScript("$(arguments[0]).change();",zoneDropDown.getOptions().get(j));
        WebElement stateDropDownElement = driver.findElement(By.xpath("//select[@id='ddlDealerState'] | //select[@name='ddlDealerState']"));
        //stateDropDownElement.click();
        jse.executeScript("$(arguments[0]).change();", driver.findElement(By.xpath("//select[@id='ddlDealerState'] | //select[@name='ddlDealerState']")));

        Select stateDropDown = new Select(stateDropDownElement);
        List<WebElement> sdd = stateDropDown.getOptions();
        for (int k = 0; k < sdd.size(); k++) {
            System.out.println("        " + sdd.get(k).getText());
        }

click()应该触发了javascript _doPostBack(zone,'')方法。跟踪也无济于事。

jse.executeScript("_doPostBack(arguments[0],'');", zdd.get(j).getText());

我得到以下输出。北部和东部区域重复相同的状态

    North
        Chandigarh
        Delhi
        Haryana
        Himachal Pradesh
        Jammu and Kashmir
        Punjab
        Rajasthan
        Uttar Pradesh
        Uttrakhand
East
        Chandigarh
        Delhi
        Haryana
        Himachal Pradesh
        Jammu and Kashmir
        Punjab
        Rajasthan
        Uttar Pradesh
        Uttrakhand

1 个答案:

答案 0 :(得分:0)

JavaScript不是问题。您只需在单击区域下拉列表的下一个值后等待。尝试以下代码

private WebDriver driver;

@Test
public void printZoneAndStates() throws InterruptedException {
    // ChromeDriverManager.getInstance().forceCache().setup();
    driver = new ChromeDriver();
    driver.manage().window().maximize();

    driver.get("https://www.bharatpetroleum.com/Bharat-Petroleum-For/Business-Associates/dealership-data.aspx");
    for (int j = 0; j < getZoneDropdown().getOptions().size(); j++) {
        System.out.println(getZoneDropdown().getOptions().get(j).getText());

        getZoneDropdown().getOptions().get(j).click();
        // use implicit wait instead of an explicit one
        Thread.sleep(3000);
        WebElement stateDropDownElement = driver.findElement(By.xpath("//select[@id='ddlDealerState'] | //select[@name='ddlDealerState']"));

        Select stateDropDown = new Select(stateDropDownElement);
        List<WebElement> sdd = stateDropDown.getOptions();
        for (int k = 0; k < sdd.size(); k++) {
            System.out.println("        " + sdd.get(k).getText());
        }
    }
}

private Select getZoneDropdown() {
    return new Select(driver.findElement(By.xpath("//select[@id='ddlDealerZone'] | //select[@name='ddlDealerZone']")));
}

当我在区域下拉列表中遇到一些StaleElementReferenceExceptions时,我决定在每次使用时都获取一个新实例。为了进行测试,我明确地等待了3秒钟。看一下隐式等待,并使用它们来获得性能。告诉我该代码是否也对您有用。

当然还有我的控制台输出:

North
        Chandigarh
        Delhi
        Haryana
        Himachal Pradesh
        Jammu and Kashmir
        Punjab
        Rajasthan
        Uttar Pradesh
        Uttrakhand
East
        Arunachal Pradesh
        Assam
        Bihar
        Manipur
        Meghalaya
        Mizoram
        Nagaland
        Orissa
        Sikkim
        Tripura
        West Bengal
        Jharkhand
        Bhutan
West
        Dadra and  Nagar
        Daman and Diu
        Goa
        Gujarat
        Madhya Pradesh
        Maharashtra
        Chhattisgarh
South
        Andhra Pradesh
        Karnataka
        Kerala
        Pondicherry
        Tamil Nadu
        Telangana