Forms.WebBrowser strange behavour - c#

I have a Webbrowser control that I am using to generate thumbnails of web pages.
Below is my code:
webBrowser.AllowNavigation = true;
webBrowser.Navigate(#"about:blank");
webBrowser.DocumentText = url;
if (webBrowser.Document != null)
webBrowser.Document.Write(url);
Where url is a string containing the html.
Using the above code results in webBrowser.DocumentText sometimes being populated with my html and othertimes not.
However I have found that line 3 and 5 are basically doing the same thing and webBrowser.Document.Write(url); is a much better way of writing the html to the browser, so I removed line 3 and it works everytime.
So my question is why by having line 3 does it cause it to fail occasionally, since line 5 is in effect making line 3 redundant anyway?

Docs for DocumentText say that
If you set the value of this property and then immediately retrieve it again, the value retrieved may be different than the value set if the WebBrowser control has not had time to load the new content.

Related

Retrieve HTML from page after given amount of time C# WPF

I am trying to get HTML from a page after a portion of Java executes and updates the HTML. (I know that Java continues to run while the page is open so there is no way to get the code "after" its finished). I'm trying to get the HTML from this page XBowling.com, you can see that there is a splash message before lanes load. I need to get the HTML after the lanes load so i can then look through the data to get to the Lane and then look through the lane's page data to get scores and what not.
I have been messing around with headerless browsers, I'm currently playing around with Awesomium with little success i can't get it to give me the updated version of the HTML just the original when the page first loads.
(I don't have any code because I don't have anything to show other then failed attempts to get the damn thing to work)
Install Selenium.Webdriver.Domify, Selenium.WebDriverBackedSelenium and Selenium.WebDriver.ChromeDriver using nuget and code something like
using (var driver = new ChromeDriver())
{
driver.Navigate().GoToUrl(url);
var columns = driver.Divs(By.ClassName("col-md-6"));
// here you access the elements using driver object
}

webdriver (c#) - looping page refresh until an elements style doesn't exist

I'm struggling a little with a method I need. Here is what I want to do:
I want to load a webpage
On that webpage, I want to wait until a specific element and it's style attribute contains "hidden: true;". When this style is present, the page has been fully loaded so I can continue my tests
I need to continue refreshing the webpage until the above condition is true - Please assume I need to keep refreshing. I know it seems stupid.
Here is what I'm trying
Step 1:
Navigating to a webpage (writing code from memory):
driver.Navigate().GoToUrl("http://example.com");
Step 2:
Find the style from the specific element (Writing code from memory)
Iwebelement MyElement = driver.findElement(By.id("example"));
Iwebelement MyElementStyle = MyElement.GetAttribute("style");
Step 3:
If the element has a style of "hidden: true;" on page load then do nothing else perform a page refresh until the element doesn't contain a style of "hidden: true;" (Writing code from memory)
if(MyElementStyle.Contains("hidden: true;"))
{
// do nothing
}
else
{
driver.Navigate().GoToUrl("http://example.com");
}
This appears to be working but the above code will only refresh the page one time so I need to loop rather than use an if statement.
Would anyone be able to offer a few pointers or suggest a better way of approaching this?
I am not sure what language binding you are using but looks like a mixture of C# and java and if that's the real test code it will not work.
Use do while loop to do a post check and check if the attribute has expected value or not.
IWebElement MyElement = Driver.FindElement(By.Id("example"));
string MyElementStyle = MyElement.GetAttribute("style");
do
{
Driver.Navigate().Refresh();
} while (MyElementStyle.Contains("hidden: true;"));

Text property on selenium web element is empty even though I can inspect it with Visual Studio

Without posting pages of C# code and markup, has anyone got a reason why this code
var link = _driver.FindElement(By.Id(field + "Field"));
var id = link.GetAttribute("id");
var text = link.Text;
given this markup
<a id="ForenameField" href="/MyUrl/MyFolder/MyId">3 errors</a>
Assigns an empty string to the text variable, but if I put a breakpoint on the second or third line and inspect the link variable, I can see the inner text of the element against the Text property on the inspector, it reads "3 errors", but the value of text is an empty string. It is not hidden, I can see the text if I add a watch or use quickview, any ideas?
Ok, it's my bad. Using jquery to toggle class on the div that contains the html in the question, meant that although users see the div appearing, the class that hides the div is still in the tag. A bit like this
<div class="hideThis showThis"><!-- my elements /--></div>
This makes it so that Selenium is right not give me a text value. It is strange however that the Visual studio debugger thinks that there should be a value. Visual Studio seems to go with what I can see, but Selenium is more pedantic about the hideThis class being there.
I go with the idea that if you can't see it you can't interact with it, so it is worth looking up the html graph from the element you expect to have a value to see if any class is present which would hide your element.
Feel free to recommend that I delete this rather obvious wisdom.
I know this was posted over a year ago, but I had this exact problem too and came across this thread. I was able to solve it by just waiting for the DOM to load--some elements aren't visible until the DOM is updated. So just putting Thread.Sleep(6000) or whatever after navigating to the page got it to work for me.

FlowDocumentPageViewer jumps be default to the last page

I have a FlowDocument that has been populated with a bunch of tables. Then I do,
DocumentReader = doc;
(DocumentReader is a FlowDocumentPageViewer and doc is FlowDocument)
When I do this, the control jumps to the last page. I tried DocumentReader.FirstPage() but then realized that the PageCount is 1. (The FlowDocument is really long; I see hundreds of pages in the control. Except when it is loaded I see page 344 of 344).
How can I jump to the first page?
Try using this -
this.DocumentReader.GoToPage(1);
I also have encounted this thing.After checking the viewingmode of the flowdocumnetreader,i find that switching the viewmode from scroll to the page
will always set the first pgae as the current page .In fact, u can set flowdocumentreader's
visibility to visible until content inserting is completed to gain a decent UI transition.
this.docReader.ViewingMode = FlowDocumentReaderViewingMode.Scrol
this.docReader.ViewingMode = FlowDocumentReaderViewingMode.Page;
good luck!

asp.net dynamically add usercontrols and position them

My issue is that I have a designer that will create a custom aspx page bu without any .net controls. I need a way of adding the controls dynamically. So far the only types of controls will be textboxes and a button, but there are 30 variations of what the textboxes can be (name, phone #, email, etc). Also the textboxes may or may not need to be required. Once the textboxes are added the form will be submitted to a db.
My first thought was to have the designer place something like [name] and then replace that with a user control that has a name textbox and a required field validator. In order to determine if the validator should be enabled I was thinking that the place holder could look like this, [name;val] or [name;noval]. I could either do replace the place holders in code dynamically or set up a tool that the user pastes their html into a textbox and clicks a button which then spits out the necessary code to create the aspx page.
I'm sure there must be a better way to do this but its a fairly unique problem so I haven't been able to find any alternatives. Does anyone have any ideas?
Thanks,
Kirk
IF your designer gives you html pages, just create a new website. copy and pages all the HTML pages with the Image folders and everything to your project. then for every HTML page create an aspx page, (with the same name) copy and pages the html's tags which are between to the aspx page's and for the body copy and paste HTML page's tags which are between into the of the aspx page.
Now you have your aspx page, exactly the same as html page.
Sounds like an attempt to over-engineer a solution to what should be a non-issue.
As #Alessandro mentioned in a comment above, why can't the designer provide you with pages that have the control markup? As it stands right now, the designer isn't providing you with "a custom aspx" so much as "a custom html page." If the designer is promising ASPX but delivering only HTML, that's a misinterpretation somewhere in the business requirements.
However, even if the designer is rightfully providing only HTML, there shouldn't be a problem with that. At worst, you can set each element you need on the server to runat="server" to access them on the server-side. Or, probably better, would be to simply replace them with the ASPX control markup for the relevant controls.
Write a simple parser that will recognize the [...] tags and replace them with corresponding controls. Its pretty easy to do and i've often done this... the tag i use is usually $$(..); though, but that doesn't matter as long as your parser knows your tags.
Such a parser will consist of a simple state-machine that can be in two states; text-mode or tag-mode. Loop through the whole page-text, char for char. As long as you're in text-mode you keep appending each char into a temporary buffer. As soon as you get into tag-mode you create a LiteralControl with the content of the temporary buffer and add it to the bottom of your Control-tree, and emtpy the buffer.
Now, you still keep adding each char into the buffer, but when you hit text-mode again, you analyze the content of the buffer and create the correct control - could be a simple switch case statement. Add the control to the bottom of your control tree and keep looping through the rest of the chars unto you read the end and keep switching back and forth between text-mode and tag-mode adding LiteralControls and concrete controls.
Simple example of such a parser... written in notepad in 4 minutes, but you should get the idea.
foreach (var c in text)
{
buffer.Append(c);
if (c== '[' && mode == Text)
{
mode = Tag;
Controls.Add(new LiteralControl(buffer));
buffer.Clear();
}
if (c == ']' && mode == Tag)
{
mode = Text;
switch (buffer)
{
case "[name]": Controls.Add(new NameControl());
... the rest of possible tags
}
buffer.Clear();
}

Categories

Resources