0

I have a web page where there is a cascaded dropdown.(See screenshot:) Cascaded Select

The second one's data look like getting generated via ajax depending on selection of the first one. Here are the codes of the first select:

<select class="form-control input-sm" name="aff_network_id" id="aff_network_id" onchange="load_aff_campaign_id($(this).val(), 0); load_landing_page(this.value); load_text_ad_id(this.value);">
    <option value="0"> -- </option>
    <option value="1">Sales</option>
</select>

And For the second select, here is the code:

<select class="form-control input-sm" name="aff_campaign_id" id="aff_campaign_id" onchange="load_text_ad_id(this.value); if($('#landing_page_style_type')){load_landing_page( $('#aff_campaign_id option:selected').val(), 0, $('input:radio[name=landing_page_type]:checked').val()?$('input:radio[name=landing_page_type]:checked').val():'landingpage');} if($('#unsecure_pixel').length != 0) { change_pixel_data();}">
            <option value="0"> -- </option> 
            <option value="1">iPhone on Amazon · $100.00</option>        
</select>

Now using web driver, when I do this:

            WebDriver driver=new FirefoxDriver();
            JavascriptExecutor js=null;
            if (driver instanceof JavascriptExecutor) {
                js = (JavascriptExecutor)driver;
            }

js.executeScript("document.getElementById('aff_campaign_id').value='1';" +
                            "return document.getElementById('aff_campaign_id').value;")

This does not change the value of the second cascaded select. It always returns "--",i.e. the default value. What can be done here to change the value of the select?

1
  • Did you try using the Select() class to set the value of the first <select> element? Commented Aug 28, 2014 at 14:51

2 Answers 2

3

If I correctly inderstood problem why don't select value in first dropDown using webdriver, and then (after value in second changes) get value in second comboBox?! e.g. smth like

//First click on first comboBox
var dropDown = driver.findElement(By.Css("#aff_network_id"));
dropDown.click();
//Select value from first dropDown
dropDown.findElement(By.Css("option[value='1']")).click();

//after that value in second dropDown will be changed and you can get it's value
driver.findElement(By.Css("#aff_campaign_id")).getAttribute('value');
Sign up to request clarification or add additional context in comments.

2 Comments

Does not work. The second cascaded dropdown has elements generated by ajax. While first one is static.
What kind of problem occurs? Webdriver cannot find second element? Or option from that? After selecting value in second dropDown using methods described before, will second dropDown be updated?
1

Developers might have might have binded an event with first drop down. I mean, after selecting option from first drop down on onblur event they might generate the dependency values in second drop down.

So what you need to do is you need to fire an event on first drop down after choosing the option.

1 Comment

Exactly! After invoking onchange event of each dropdown and putting a wait of 1 sec for script to complete, it works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.