Imagine a web site, where there are 2 drop down controls like min age and max age. After I choose min age and max age, I would like to hit the search button. How do I get the search results into an output stream and then I could navigate the DOM HTML. I already know how to naviate through dom html via htmlagilitypack. I just do not know how to invoke the drop down make selections and invoke search button. I want to do all this from console application. Is it even possible? I am using C# visual studio 2010.
Thanks..
If the html looks like this:
<form action="someurl">
<select name="SomeOption">
<option value="val1">val1</option>
<option value="val2">val2</option>
</select>
</form>
And you want to submit the form with "val1", then you need to execute an HTTP POST operation to "someurl" including the argument SomeOption=val1. That is essentially what the browser is doing. Of course the form you are trying to simulate will be a bit more complicated.
Is that what you were asking?
BTW, if you want to see exactly what is going on when the browser submits the form, check out Fiddler, an excellent tool made by a colleague at Microsoft.
Related
So I'm trying to scrape a website using AngleSharp and want to access a particular button that is nested deep in the site. I have logged out the parsed document html with document.DocumentElement.OuterHtml
but can only see so far into the document:
<div class="l-propertySearch-paginationAndSearchFooter" data-test="pagination">
<div data-bind="component: 'pagination'"></div>
</div>
</div>
However, when I inspect the page in the web browser, I can see the additional layers necessary to access the button:
As you can see, the div with the data-bind attribute title "component: 'pagination'" open up further but doesn't display this in the log - this is why, I suspect, I can't retrieve the element.
I've experimented with document.QuerySelectorAll("button" and get back a list of buttons but not the one I'm after - it's like the particular block I want doesn't exist. Any ideas what I'm doing wrong?
As far as I understand that button you are looking for is created with javascript and does not exist in original source code. That is the reason you can't access that button with anglesharp. Right click on website and click View page source (Ctrl + U on chrome) and look for your button there. That is what anglesharp sees not html inside inspect element.
I am trying to automate a download function from a broker. I have been able to logon with id and pw, navigate to specific web pages, and select and collect items from the site. I am stumped, however, on how to select an ARIA:Listbox item in an embedded form. All the examples I have seen show windows forms listbox or HTML select lists which is not the way this www site is built.
I have been successful with the c# Selenium findelement By.ID for the listbox required, but the last error said it must be scrolled into view. I'm guessing that the item is not in view because I haven't got the ARIA Listbox click to work to display the list. I've tried clicking on the dropdown arrow, and on the cell itself but the list doesn't show up.
I need some pointers on what to try next.
Thanks
OK - here's some additional info:
I apologize if this doesn't present right - I'm just learning to use the site...
When I use the program code
browser_driver.FindElement(By.Name("OfxDownloadForm:downloadOption")).Click();
I get the message
OpenQA.Selenium.ElementNotInteractableException: 'Element could not be scrolled into view'
The relevant html follows:
<span id="OfxDownloadForm:downloadOption" role="presentation" onfocusjs="removingDuplicateContentFromMenu();;vg.validation.focus(this)" onchangejs="setWarnings('downloadOption');" onblurjs="vg.validation.blur(this)" class="vg-SelOneMenu" compname="selectOneMenu">
<div class="vg-SelOneMenuCont vg-SelOneMenuNoWrap vg-SelOneMenuFocusText vg-SelOneMenuHover" id="OfxDownloadForm:downloadOption_cont" aria-owns="menu-OfxDownloadForm:downloadOption">
<a href="javascript:void(0);" onclick="return false;" class="vg-SelOneMenuTrigger" id="OfxDownloadForm:downloadOption_aTag" role="button" aria-xpanded="false" aria-haspopup="true" aria-controls="menu-OfxDownloadForm:downloadOption" aria-labelledby="downLoadOptionText OfxDownloadForm:downloadOption_text" aria-disabled="false">
If I go after "OfxDownloadForm:downloadOption_aTag" with:
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.PresenceOfAllElementsLocatedBy(By.ID("OfxDownloadForm:downloadOption_aTag")));
browser_driver.FindElement(By.ID("OfxDownloadForm:downloadOption_aTag")).Click();
Nothing happens - the popup doesn't occur.
Hope this comes out ok!
After much research and reading, I figured out that I had to do a two step action in the program:
input_element = browser_driver.FindElement(By.Id("OfxDownloadForm:downloadOption_aTag"));
Actions actions = new Actions(browser_driver);
actions.MoveToElement(input_element);
actions.Perform();
input_element.Click();
I believe the FindElement method just located the element, but didn't reposition the mouse into the document on that element. MoveToElement method scrolled down the document and brought the element into the view, and then a click on the same element allowed the drop-down listbox to do its processing. Once I did the find and the move, I was able to select the appropriate dropdown item.
I have a fairly simple page with a set of jQuery tabs, the content of some is called via ajax. I also have a search box in the masterpage in my header.
When I open the tabbed page the search box works fine. However once I have clicked on one of the ajax tabs the search box fails to work with an "Invalid Viewstate" yellow screen of death.
I believe this is because the ajax page is replacing the __VIEWSTATE hidden input with its own.
How can I stop this behaviour?
UPDATE: I have noticed that the YSOD only appears in IE and Chrome, Firefox doesn't seem to have the same issue. Although how the browser influences the ViewState, I'm not sure.
UPDATE: I've put a cut down version of the site that shows the issue here: http://dropbox.com/s/7wqgjqqdorgp958/stackoverflow.zip
The reason of such behavior is that you getting content of the ajaxTab.aspx page asynchronously and paste it into another aspx page. So you getting two instances of hidden fields with __VIEWSTATE name and when page posted back to server theirs values are mixing (might depends on how browser process multiple controls with same name on submit). To resolve this you can put second tab's content into a frame:
<div id="tabs">
<ul>
<li>Default Tab</li>
<li>ajax Content</li>
</ul>
<div id="tabs-1">
<p>
To replicate the error:
<ul>
<li>First use the search box top right to search to prove that code is ok</li>
<li>Then click the second ajax tab, and search again.</li>
<li>N.B. Chrome / IE give a state error, Firefox does not</li>
</ul>
</p>
</div>
<iframe id="tabs-2" src="ajaxTab.aspx" style="width:100%;" ></iframe>
</div>
Also, I'm not sure but this seems like error in the Web_UserControls_search control. In my opinion, NavBarSearchItemNoSearchItem_OnClick method must be refactored as below:
protected void NavBarSearchItemNoSearchItem_OnClick(object sender, EventArgs e)
{
var searchFieldTbx = NavBarSearchItemNo;
var navBarSearchCatHiddenField = NavBarSearchCatHiddenField;
var term = searchFieldTbx != null ? searchFieldTbx.Text : "";
if (term.Length > 0) //There is actually something in the input box we can work with
{
//Response.Redirect(Url.GetUrl("SearchResults", term));
Response.Redirect(ResolveClientUrl("~/Web/SearchResults.aspx?term=" + term + "&cat=" + navBarSearchCatHiddenField.Value));
}
}
Draw attention that we resolving client url when redirecting to search results page and instead of navBarSearchCatHiddenField use navBarSearchCatHiddenField.Value as cat parameter.
I guess that you use AJAX to fill the content of the tab. So in this case, content of your tab will be replaced by the new one from ajax and certainly _VIEWSTATE will be replaced. At server, do you use data from ViewState? In the "static tabs", you should prevent them auto reload by using cache:true
Your issue is that with your ajax call you bring in a complete ASPX page. Including the Form tag and its Viewstate. If you remove the Form tag from ajaxTab.aspx you will see everything works fine. asp.net does not know how to handle two Form tags in one page. Same goes for hidden Viewstate fields. You cannot bring in a full aspx page via ajax. Just bring in the content Div you want to display and you`ll be good to go.
I am working on a simple web forms application with C# (Microsoft Visual C# 2010 Express).
I have two text boxes (textBox1, richTextBox1) a button (button1) and a web browser (webBrowser1) on the form. The web browser goes to a web page when I run the program. On that page there are two input fields that I want to autofill with the click of the button1 using the text in textBox1 and richTextBox1.
You can see the code of the input fields on that web page:
<input type="text" id="subject" tabindex="4" name="subject" value="">
<textarea class="composebody" tabindex="6" name="message" id="message" rows="20" cols="80"></textarea>
I know this is very simple, but I don't have much knowledge about C#. Any ideas how I can code that?
Thanks.
You need to write this code
webBrowser1.Document.GetElementById("subject").SetAttribute("value", subject.text);
webBrowser1.Document.GetElementById("msg").SetAttribute("value",message.text );
and need to call those two lines in
DocumentCompleted event of webbrowser.
Hope it helps.
I believe you are looking for the following:
subject.value = "Your info here";
This will solve the issue for your first item but the text area is a bit more tricky. You will probably need to include some HTML item inside the text area that you can write to. I was not able to find a good way to write to the textarea item easily. If possible, I would suggest using a different control.
We are developing an ASPX (.NET 2.0) page that includes a select list with far too many elements in it (200+).
We need some form of autocomplete to make this into something that behaves like a text box, but with autocompelte suggestions.
We would like to use JQuery. So far our searching has only turned up autocompletes that require some kind of back end service, additional requests (in AJAX) etc.. We would prefer to deliver all the data at once with the page request. Ideally it would be as select list entries.
Are there autocomplete boxes that convert a select list? or is there a way to wire an autocomplete to data already on the page (in ASPX with .NET 2.0) such that it will not have to access external resources?
Edit: Postbacks was not the phrasing I was looking for. I mean delivered with the original page request.
Edit 2: The page should degrade gracefully. Many of the solutions out there 'inject' the content, so without javascript you don't get any content. It may be 200+ choices, but at least they would exist there. That is why the conversion of a select list is our ideal.
Use the jQuery autocomplete plugin as suggested by Steve Willcock. Output a regular select-box, then in your script replace it with a textbox and initialize the plugin with an array that you build out if the option-elements. So your data comes in as a select:
<select size="1" id="options" name="options">
<option>Option #1</option>
<option>Option #2</option>
<option>Option #3</option>
<option>Option #4</option>
<option>Option #5</option>
<option>Option #6</option>
<option>Option #7</option>
</select>
...and you transform it like so:
$(function(){
// execute once the DOM is ready...
// build array of option texts
var options = [];
$("#options option").each(function(){
options.push($(this).text());
});
// build an input field, replace the select with it,
// and wire up autocomplete.
$("<input id='options' name='options' type='text'>")
.replaceAll("#options")
.autocomplete(options, {autoFill: true});
});
...no JS? No problem - you still have your select.
http://docs.jquery.com/Plugins/Autocomplete/autocomplete#url_or_dataoptions
The jquery autcomplete plugin can take data as an array. If you build the array in a .js include file that should suit your requirements.
For an example, take a look at the demo page - check the "Multiple Cities (local)" section, and the localdata.js file that is used there.
There are a number of third party controls (we use one from ComponentArt's WebUI toolkit) that will give you the auto-complete combobox functionality.
You can also achieve the same idea using AJAX techniques so that you avoid a full postback.
The answers here were very helpful, but we found this plugin after further searching that works automatically directly on the select list:
Sexy Combo: http://code.google.com/p/sexy-combo/
Demo page: http://phone.witamean.net/sexy-combo/examples/index.html
This will require a bit less work.
I wanted to document this for the reference of others. Thank you all for the help.