Re: [selenium-users] Selenium WebDriver C# select from drop down menu does not trigger Ajax

4,862 views
Skip to first unread message

Moises Siles

unread,
Apr 2, 2013, 4:13:26 PM4/2/13
to seleniu...@googlegroups.com
I'm not sure if this is the best solution or if this could work, but could you try after that something like

select.Sendkeys(enter key)

to see if that triggers the action


On Tue, Apr 2, 2013 at 1:34 PM, Qian Xie <tama...@gmail.com> wrote:
I want to select an option from a drop down menu that looks like this:

<select id="CostType" onchange="javascript:setTimeout('__doPostBack(\'CostType\',\'\')', 0)" name="CostType">
<option value="">- Select One -</option>
<option value="Internal" selected="selected">Internal Cost</option>
<option value="External">Project Cost</option>
</select>

I have tried using the following:
SelectElement select = new SelectElement(CostType);
          select.SelectByText("Internal Cost");

Using the above code, selenium will change the the correct option in the drop down menu. But it does not trigger Ajax that will get data from relevant units and then populate some other elements on the webpage. I also added wait after the select, but that does not solve the problem.
This works fine if I do the process manually. I am using Selenium WebDriver C# on IE. I have tried Java on IE with the Select class and it worked fine. But it does not work when I'm using C#.
Can anyone help me out with this? Thank you so much!

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/owtQTG7ygEYJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Moises Siles

unread,
Apr 2, 2013, 5:33:47 PM4/2/13
to seleniu...@googlegroups.com
errors or something? or  nothing happens?


On Tue, Apr 2, 2013 at 2:26 PM, Qian Xie <tama...@gmail.com> wrote:
msiles, I tried it and it does not work.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/uNCS0ZwA_ksJ.

Kartik Shah

unread,
Apr 3, 2013, 1:09:09 AM4/3/13
to seleniu...@googlegroups.com
In this case, try the hard way of actually clicking the element using something like:
1. Click the drop down arrow button so following values will be displayed in the drop down
2. Click the element using driver.FindElement(By.Xpath("//option[text()='Internal Cost']")).click();

If selenium click does not work, try using javascript option

Kartik Shah


On Wed, Apr 3, 2013 at 3:18 AM, Qian Xie <tama...@gmail.com> wrote:
nothing happened. 
This line select.SelectByText("Internal Cost") will highlight the internal cost option and select it. But it does not trigger ajax like it would if you are doing it manually.
I did a sendkeys(keys.enter) like you suggested. It did not do anything.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/0rOzBxgyTeIJ.

Qian Xie

unread,
Apr 3, 2013, 9:08:01 AM4/3/13
to seleniu...@googlegroups.com
Hi Kart,

I have tried your suggestion. But it didn't work. Internal Cost option is selected. But Ajax is still not triggered.
I'm not sure how to use the javascript option. Could you help me with that?
Thanks!

Qian

Moises Siles

unread,
Apr 3, 2013, 9:34:18 AM4/3/13
to seleniu...@googlegroups.com
Can we see more of your code and also part of the html?


To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/_stwEwxzHEcJ.

Qian Xie

unread,
Apr 3, 2013, 10:20:51 AM4/3/13
to seleniu...@googlegroups.com
I'm not sure what other information is relevant. But here is the HTML

<tr id="trCostType">
<td id="TableCell1" class="formElement">
<span id="labelCostType">*Cost Type:</span>
</td>
<td id="TableCell2" class="formElement">
<select id="ddlCostType" onchange="javascript:setTimeout('__doPostBack(\'ddlCostType\',\'\')', 0)" name="ddlCostType">
<option value="">- Select One -</option>
<option value="Internal" selected="selected">Internal Cost</option>
<option value="External">Project Cost</option>
</select>
<span id="rfvDdlCostType" style="color:Red;display:none;">Required Field</span>
</td>
</tr>

my code:

public void setCostType(String type)
        {
  //I have tried this//
            CostType.Click();
            selenium.FindElement(By.XPath("//option[text()='Internal Cost']")).Click();
            CostType.SendKeys(Keys.Enter);

            //And this//
            /*SelectElement select = new SelectElement(CostType);
            select.SelectByText(type);*/


            WebDriverWait wait = new WebDriverWait(selenium, TimeSpan.FromSeconds(10));
            wait.Until(d=>LegalEntity);
            
        }

Thank you!

Kartik Shah

unread,
Apr 3, 2013, 10:25:54 AM4/3/13
to seleniu...@googlegroups.com
Try this

IWebElement element= driver.FindElement(By.Xpath("//option[text()='Internal Cost']"));
                if (element != null)
                    ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", element);

Kartik Shah


To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/_stwEwxzHEcJ.

Kartik Shah

unread,
Apr 3, 2013, 10:31:13 AM4/3/13
to seleniu...@googlegroups.com
Qian - One thing i noticed in the HTML you posted was; the element you are trying to click, is already selected. If that is the case there is a change, ajax will not be triggered as it is already selected. 

Can you try clicking any other option from the list and see if that works for you?

Kartik Shah

Qian Xie

unread,
Apr 3, 2013, 10:53:57 AM4/3/13
to seleniu...@googlegroups.com
Hi Kart,

I tried the Javascript option you suggested. It didn't select the "Internal Cost". And I'm pretty sure it found the element and it's not null. The program just timed out waiting in the end.

As for the other suggestion, the webpage by default is set to the "Select One" option. After you choose the "Internal Cost", ajax will be triggered. The HTML I provided was when "Internal Cost" is selected manually. I have also tried using selenium to select other option. But it didn't work either.

Qian Xie

unread,
Apr 4, 2013, 9:45:06 AM4/4/13
to seleniu...@googlegroups.com
Can anyone help me with this please? It seems like Javascript is the only option. But I'm not very familiar with it.

Arran

unread,
Apr 4, 2013, 9:52:15 AM4/4/13
to seleniu...@googlegroups.com
We need to see a working page.

You keep saying the 'ajax' isn't fired, well what kind? Is it some kind of event? Looking at the HTML you provided, there is an onchange event, but this looks coded from the code-behind of the page. Can you show us the source of this event?

Have you tried this in other browsers?

Qian Xie

unread,
Apr 4, 2013, 1:31:28 PM4/4/13
to seleniu...@googlegroups.com
The onchange event should be fired after I choose an option in the dropdown. And it will generate a new set of dropdown menus on the page. 
I have tested using C# on Firefox. And Java in IE and Firefox. They all work fine. The only time it doesn't work is when I'm using C# on IE.
Please let me know if you need more information.
Thanks!

Qian Xie

unread,
Apr 4, 2013, 2:05:30 PM4/4/13
to seleniu...@googlegroups.com
The only workaround I found right now is after using Select to choose the option in the dropdown menu, do a sendkeys(key.up) and a sendkeys(key.down). That will trigger the onchange event. But is there a better way to do this...?

Jim Evans

unread,
Apr 4, 2013, 5:19:36 PM4/4/13
to seleniu...@googlegroups.com
One piece of information I can't seem to find in this thread is what version of the .NET bindings, and which version of IEDriverServer.exe.
Message has been deleted

Qian Xie

unread,
Apr 5, 2013, 9:43:23 AM4/5/13
to seleniu...@googlegroups.com
I'm using 2.31.0 for both .Net bindings and IEDriverServer

Qian Xie

unread,
Apr 5, 2013, 10:37:30 AM4/5/13
to seleniu...@googlegroups.com
I also tried using .Net bindings 2.31.2. Still doesn't work. Could anyone please help me look at this issue?
Thanks!

Jim Evans

unread,
Apr 5, 2013, 11:23:28 AM4/5/13
to seleniu...@googlegroups.com
Can you provide a full page, and not just the snippets of HTML code describing the <select> element you've provided so far? Note that "full page" would also include working JavaScript hooked up to the appropriate events to demonstrate your issue. If not, the answer is likely to be, "No, no one can help you with this issue."

Under the covers, at the wire protocol level, the .NET bindings and the Java bindings should be using the exact same methods to communicate with the IEDriverServer.exe. An examination of the bindings' source code confirms this. Moreover, I've tested several similar scenarios with .NET and IE, and they've worked flawlessly. So if you want me to have a proverbial snowball's chance in hell of debugging the issue, I'll need a full, complete repro case.

Oh, and before you reply why supplying a repro case is impossible, head over to http://jimevansmusic.blogspot.com/2012/12/not-providing-html-page-is-bogus.html and give it a read. If your reasons for insisting a repro page is impossible are one of these, then it's not that you can't supply one, it's just that you aren't willing to.

Qian Xie

unread,
Apr 5, 2013, 2:54:40 PM4/5/13
to seleniu...@googlegroups.com
Hi Jim,

I have attached the full page. But I have very limited knowledge of Javascript. If I missed something, I'm sorry. Please let me know, and I will get it.
Thanks!

Qian



--
You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/nwWwPHU7Chs/unsubscribe?hl=en-US.
To unsubscribe from this group and all its topics, send an email to selenium-user...@googlegroups.com.

To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/T7QJ0Bb8JGwJ.
FullPage.aspx

Madan Singh

unread,
Apr 6, 2013, 12:25:15 AM4/6/13
to seleniu...@googlegroups.com
Dear Qian,
please try to use this code,
to select value from drop down in c#
 new SelectElement(driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_DDLMsg"))).SelectByValue("message");

Qian Xie

unread,
Apr 8, 2013, 9:58:54 AM4/8/13
to seleniu...@googlegroups.com
Hi Madan,

I tried using SelectByValue as you suggested. It selects the correct option just like SelectByText, but still does not trigger the onchange event.
Any other suggestions?
Thanks!

Qian

Qian Xie

unread,
Apr 8, 2013, 4:01:38 PM4/8/13
to seleniu...@googlegroups.com
Can anyone help me with this issue please? I have posted the full page in a previous post. I've searched everywhere but couldn't find a solution that works....

Jim Evans

unread,
Apr 8, 2013, 4:28:20 PM4/8/13
to seleniu...@googlegroups.com
You posted *something*, I'll grant you that. Tell me this: Are you able to copy the page that you posted into a locally running web server, browser to it, and reproduce your issue? I'd venture to say you can't. There's lots of "__doPostBack" calls in the source of the page you've provided. I don't know how to load this page into a web server, and verify that the AJAX call is or isn't being called, because the back-end processing of the system you're linked to isn't available. Nor do I have the JavaScript source files referenced in the source code of the page you posted. Even attempting to use IIS to host the page yields a ton of errors just browsing to the page in IE.

At this point, you have two choices. First, you can make the *actual* page available that reproduces the problem. Note that if this is an internal application (which it most likely is, given the tone of the questions you've been asking so far), you'll need to fix the system so that an external browser can access the page. Second, you can take the page you posted, and start stripping out extraneous items, until you're left with a page that can be dropped into a stock web server, that still reproduces the issue.

Qian Xie

unread,
Apr 10, 2013, 1:07:45 PM4/10/13
to seleniu...@googlegroups.com
Hi Jim.

Like you suggested, I narrowed down the problem and reproduced it on my local machine. I have attached the full page and the JavaScript files that are causing the problems.
Without the two JavaScript files, my selenium test will work fine. It will choose the option in the dropdown and trigger the onchange event. You will see IE tries to go to a new page and shows "IE cannot display the web page" (since the page is not included). This is the expected behavior. 
However, if you include the two JavaScript files attached, selenium will choose the option in the dropdown and sits on that page; onchange event is not triggered in this case. In either case (with or without the Javascript files), choosing the dropdown manually by mouse click will trigger the onchange event. 
There are several other JavaScript files mentioned in the HTML, but only the JavaScript files attached will cause selenium to not fire the onchange event.

Below is the selenium code I used to test it.
IWebDriver driver = new InternetExplorerDriver(IEurl);
driver.Navigate().GoToUrl(url);
IWebElement ele = driver.FindElement(By.Id("ctl02_eigdAddItemGroupReport_ddlCostType"));
SelectElement select = new SelectElement(ele);
select.SelectByValue("Internal");


When I change the following line in FullPage.html:
<select name="ctl02$eigdAddItemGroupReport$ddlCostType" onchange="javascript:setTimeout('__doPostBack(\'ctl02$eigdAddItemGroupReport$ddlCostType\',\'\')', 0)" id="ctl02_eigdAddItemGroupReport_ddlCostType">

to 

<select id="ctl02_eigdAddItemGroupReport_ddlCostType" name="ctl02$eigdAddItemGroupReport$ddlCostType"
                                onchange="alert('Hello')">

only ScriptResources.axd will cause the problem. ScriptResource3.axd doesn't.

The ScriptResources.axd file mentioned about onchange. The ScriptResource3.axd mentioned about postback. I assume that's why I'm seeing the above behavior. But I don't know enough about JavaScript to figure out a solution. I would really appreciate any help I can get. If there is other relevant information I didn't provide, please let me know. I will do my best.
Thanks!

Qian


--
You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/nwWwPHU7Chs/unsubscribe?hl=en-US.
To unsubscribe from this group and all its topics, send an email to selenium-user...@googlegroups.com.

To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/gizIMx9JpXUJ.
FullPage.html
ScriptResource.axd
ScriptResource3.axd

Qian Xie

unread,
Apr 11, 2013, 1:23:32 PM4/11/13
to seleniu...@googlegroups.com
Can someone please help me look at this issue? Please let me know if there is other information you would need.
Thanks!


On Wednesday, April 10, 2013 1:07:45 PM UTC-4, Qian Xie wrote:
Hi Jim.

Like you suggested, I narrowed down the problem and reproduced it on my local machine. I have attached the full page and the JavaScript files that are causing the problems.
Without the two JavaScript files, my selenium test will work fine. It will choose the option in the dropdown and trigger the onchange event. You will see IE tries to go to a new page and shows "IE cannot display the web page" (since the page is not included). This is the expected behavior. 
However, if you include the two JavaScript files attached, selenium will choose the option in the dropdown and sits on that page; onchange event is not triggered in this case. In either case (with or without the Javascript files), choosing the dropdown manually by mouse click will trigger the onchange event. 
There are several other JavaScript files mentioned in the HTML, but only the JavaScript files attached will cause selenium to not fire the onchange event.

Below is the selenium code I used to test it.
IWebDriver driver = new InternetExplorerDriver(IEurl);
driver.Navigate().GoToUrl(url);
IWebElement ele = driver.FindElement(By.Id("ctl02_eigdAddItemGroupReport_ddlCostType"));
SelectElement select = new SelectElement(ele);
select.SelectByValue("Internal");


When I change the following line in FullPage.html:
<select name="ctl02$eigdAddItemGroupReport$ddlCostType" onchange="javascript:setTimeout('__doPostBack(\'ctl02$eigdAddItemGroupReport$ddlCostType\',\'\')', 0)" id="ctl02_eigdAddItemGroupReport_ddlCostType">

to 

<select id="ctl02_eigdAddItemGroupReport_ddlCostType" name="ctl02$eigdAddItemGroupReport$ddlCostType"
                                onchange="alert('Hello')">

only ScriptResources.axd will cause the problem. ScriptResource3.axd doesn't.

The ScriptResources.axd file mentioned about onchange. The ScriptResource3.axd mentioned about postback. I assume that's why I'm seeing the above behavior. But I don't know enough about JavaScript to figure out a solution. I would really appreciate any help I can get. If there is other relevant information I didn't provide, please let me know. I will do my best.
Thanks!

Qian
On Mon, Apr 8, 2013 at 4:28 PM, Jim Evans  wrote:
You posted *something*, I'll grant you that. Tell me this: Are you able to copy the page that you posted into a locally running web server, browser to it, and reproduce your issue? I'd venture to say you can't. There's lots of "__doPostBack" calls in the source of the page you've provided. I don't know how to load this page into a web server, and verify that the AJAX call is or isn't being called, because the back-end processing of the system you're linked to isn't available. Nor do I have the JavaScript source files referenced in the source code of the page you posted. Even attempting to use IIS to host the page yields a ton of errors just browsing to the page in IE.

At this point, you have two choices. First, you can make the *actual* page available that reproduces the problem. Note that if this is an internal application (which it most likely is, given the tone of the questions you've been asking so far), you'll need to fix the system so that an external browser can access the page. Second, you can take the page you posted, and start stripping out extraneous items, until you're left with a page that can be dropped into a stock web server, that still reproduces the issue.


On Monday, April 8, 2013 1:01:38 PM UTC-7, Qian Xie wrote:
Can anyone help me with this issue please? I have posted the full page in a previous post. I've searched everywhere but couldn't find a solution that works....

On Monday, April 8, 2013 9:58:54 AM UTC-4, Qian Xie wrote:
Hi Madan,

I tried using SelectByValue as you suggested. It selects the correct option just like SelectByText, but still does not trigger the onchange event.
Any other suggestions?
Thanks!

Qian

On Saturday, April 6, 2013 12:25:15 AM UTC-4, Madan Singh wrote:
Dear Qian,
please try to use this code,
to select value from drop down in c#
 new SelectElement(driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_DDLMsg"))).SelectByValue("message");

--
You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/nwWwPHU7Chs/unsubscribe?hl=en-US.
To unsubscribe from this group and all its topics, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.

Jim Evans

unread,
Apr 11, 2013, 9:30:52 PM4/11/13
to seleniu...@googlegroups.com
After some investigation, and thanks to the posting of the resources earlier in the thread, I've been able to track down the source of the problem. The issue comes into play when you're using IE9 or IE10, and will affect all language bindings (Java, Ruby, Python, or .NET). The JavaScript framework that your application is using is doing something to prevent the IE driver from triggering the onchange event using W3C DOM events. This is the preferred way to trigger events in browsers that support it, like IE9 and IE10. Furthermore, the issue is dependent on the web server used to server your pages. To prove this is the case, do the following:

1. Open your test page in IE9
2. Open the F12 Developer Tools by pressing the F12 key on your keyboard
3. Go to the Script tab in the F12 Developer Tools
4. Type the following script into the console, one line at a time, and watch what happens:

var selectElement = document.getElementById('ctl02_eigdAddItemGroupReport_ddlCostType');
selectElement[1].selected = true;
var evt = document.createEvent('HTMLEvents');
evt.initEvent('change', true, false);
selectElement.dispatchEvent(evt);

When you execute the second line, you should see "Internal Cost" selected in the drop down. When you execute the last line, you'll either be taken to the next page (that is, the onchange event will be triggered), or you'll receive an error in the console that says something similar to "Unable to get property 'srcElement' of undefined or null reference." If your page is hosted on IIS, or the Visual Studio Development Web Server, you'll probably see everything succeed. If you're using another web server, you'll likely receive the error. There's some magic happening in the ASP.NET pieces that make up your web application.

This hopefully gives you some insight into what's happening. As for how to fix it or resolve the issues, I have no idea. You could be running on an older version of the ASP.NET framework, which may already have this fixed. At any rate, the JavaScript code I listed above is a stripped-down version of what the IE driver does for IE9 and IE10. If you can get that to work from the F12 Developer Tools, it should work in the IE driver. If that script doesn't work, the IE driver won't work either, and there's something broken in your application that's preventing DOM events from working properly.
To unsubscribe from this group and all its topics, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.

Qian Xie

unread,
Apr 17, 2013, 6:11:58 PM4/17/13
to seleniu...@googlegroups.com
Hi Jim,

Thanks for looking into it. But I don't think it's the problem of my web server. We are using IIS, ASP.NET framework 2.0. The two javascript files that are causing problems are generic, and are generated by IIS.
This issue only shows up when the drop down list is in a UpdatePanel. Drop down lists outside UpdatePanel work fine. The UpdatePanel does a partial postback when the dropdown list is changed.
I have seen this problem in both IE8, IE9. And it only happens with the .NET binding. I didn't see the problem with the Java binding.
I'm still trying to figure out the cause of the problem. If you have any other insight for the cause or solution, please let me know!
Thanks!

Qian


To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/4QvGHfqEyg0J.

Jim Evans

unread,
Apr 17, 2013, 11:26:37 PM4/17/13
to seleniu...@googlegroups.com
Do me a favor. Follow the debugging steps I outlined in my last reply on IE 9 or 10, and let me know what happens. When you open the F12 Developer Tools, take special note of the browser mode and document mode when you try the test, please.

Qian Xie

unread,
Apr 18, 2013, 1:26:36 PM4/18/13
to seleniu...@googlegroups.com
Hi Jim,

I tried the steps you gave me on IE 9. Browser mode IE9, Document mode IE9 standards. Just like you said, on the second line of the script, "Internal Cost" selected in the drop down. On the last line, I received an error in the console that says "SCRIPT5007: Unable to get value of the property 'srcElement': object is null or undefined ", and a value true.

Thank you! I would appreciate any advice!

Qian



On Wed, Apr 17, 2013 at 11:26 PM, Jim Evans <james.h....@gmail.com> wrote:
Do me a favor. Follow the debugging steps I outlined in my last reply on IE 9 or 10, and let me know what happens. When you open the F12 Developer Tools, take special note of the browser mode and document mode when you try the test, please.
--
You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/nwWwPHU7Chs/unsubscribe?hl=en-US.
To unsubscribe from this group and all its topics, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/dIlHu_sjp9oJ.

keshav katoch

unread,
Jul 15, 2013, 7:31:38 AM7/15/13
to seleniu...@googlegroups.com
Hi Qian,

<select id="CostType" onchange="javascript:setTimeout('__doPostBack(\'CostType\',\'\')', 0)" name="CostType">
<option value="">- Select One -</option>
<option value="Internal" selected="selected">Internal Cost</option>
<option value="External">Project Cost</option>
</select>

Try  using the following:
SelectElement select = new SelectElement(driver.FindElement(By.Id("CostType")); /// Cerating SelectElement.
          select.SelectByText("Internal Cost");

Let me know if this works for you.


Reply all
Reply to author
Forward
0 new messages