Dynamically Change Css Styles in C# (Sharp) Without Asp - c#

Hi all hopefully this is a quick one.
I'm working on a c# web browser that, amongst other things, changes css styles of web pages like google and facebook. for example it will make the background on facebook and google red instead of white. i've had success but its not at all consistent and i have no idea why.
HtmlDocument doc = Browser1.Document;
HtmlElement textElem = doc.CreateElement("DIV");
textElem.InnerHtml = "<STYLE>body{background-color:red!important}</STYLE>";
doc.Body.AppendChild(textElem);
that code works on www.google.com but not www.rationality.tk however..
HtmlElement head = Browser1.Document.GetElementsByTagName("body")[0];
head.SetAttribute("bgcolor", "red");
That code works on www.rationality.tk but not www.google.com and both codes do not work on www.facebook.com which i cannot get anything to work on.
I'm probably doing something wrong and I just moved on to c# after giving up on c++ and I have found it a lot easier but still getting the hang of it. thank you in advance.
EDIT: PROBLEM SOLVED
HtmlElement head = Browser1.Document.GetElementsByTagName("body")[0];
head.Style = "background-color:red";
this works on facebook, google and rationality.tk

I am not very sure but the problem might be due to embedded iframes and igoogle like widgets. I have faced the same problem while implementing a similar functionality in COM/ATL for my Browser Helper Object. The approach that helped me (to some extent) was to wait for DOCUMENT_COMPLETE event for each iframe and then try to get their body. Again I am not sure how you can achieve the same in C#. This is just some food for your thoughts.

Related

Xpath works in Chorome but not in Selenium web-driver

I am working on a website where all other locator doesn't work expect using FindElements and take the 3rd a element. So I was curious to try xpath the first time.
I could get the xpath in chrome, but when I use in xpath, it says element not found.
I did a lot of search, still couldn't' find out what was wrong. So I tried in facebook page and use the login field as a try, the xpath is //*[#id="email"], it works perfectly in chrome, but same result in webdrive.
C# code: driver.findElement(By.xpath("//*[#id='email']"));
Please click for facebook picture and its location
Any advise?
I can give a complete solution on Python taking into account the features of React Native (used on Facebook)
But, you have C#. Therefore, it is possible to use a similar function driver.execute_script (execution of Javascript on Selenium)
driver.get("https://www.facebook.com/")
driver.execute_script('
document.getElementById("email").value = "lg#hosct.com";
document.getElementById("u_0_2").click();
')
I did another try with a more clear code:
driver.Url = "";
driver.findElement(By.xpath("//*[#id='email']"));
It works now, the only difference between this and my code before is: I was visiting some other pages before the facebook page. This seems to make difference. Anyway, above code works. If I encounter the issue again, I will post more detail code.

Make asp.net site more attractive by flash template

I have downloaded a free flash template from http://www.flashmo.com/preview/flashmo_197_bokeh
I want to use this template in my asp.net (C#) website. More specifically speaking, I want to use that color effect ( the color-change that is happening there in the background of the page) in my webpage.. Please help me to do so..
(Please do check the link I have provided. It will help you understand the color-change I am talking about).
Thanks in advance.
This should be fairly straight forward.
Just embed the downloaded swf file in the aspx page as you would with a regular html page.
See an example on: How to embed a swf file into html code?
The hard part is going to be interacting with the swf. This is possible via Javascript, but is gona take some work. In flash action script you can expose variables to javascript. Once that is done, you can call asp.net code from javascript. Both techniques are very well documented on the net. Just google on it.
Good luck ...
Please don't use flash to create a website and don't use 'attractive' to describe a website.

Is there anyone using Xbrowser for html or aspx pages testing

There is a requirement for us to implement browser testing for html and aspx pages. I came across xbrowser at the below link.
http://xbrowser.codeplex.com/
I couldn't find any documentation on their website , it has only solution file. I downloaded the code but its not running fully.
Any help or guidence will be really appreciated.
XBrowser isn't based on Trident (IE), WebKit (Safari/Chrome), Gecko (Firefox) or any other common rendering engine so as far as "browser testing" its pretty much useless. What it is intended for is website automation. You can instantiate it in code, tell it to navigate to a webpage and then tell it various things such as "enter xyz into the search field and click the submit button". If this is what you're looking for then its also very important to understand that just because what you've tested works in XBrowser doesn't mean it will work anywhere else. It might, it might not, you won't know until you manually test every browser.
If you follow the link at the top of the codeplex site you'll see that the project has been moved to GitHub. From there the author has a link to an earlier project call SimpleBrowser that has some examples that I imagine are similar to XBrowser.

scraping/simulate browsing help

I want to make a program that will simulate a user browsing a site and clicking on links. Cookies and javascript have to be enabled. I've successfully done this in python, but I want to write it an compilable language (python ide's don't cut it). The links on the site are generated with javascript and are dynamic. With python I used PAMIE (third party module that uses win32com) to launch an instance of Internet explorer, scrape the generated html for the links, then navigate to one of them. The point is for the whole process to be transparent to the server. What's the best (compilable) language and method to do this? I was thinking C# with WebBrowser control but I don't want to spend a lot of time learning something if it isn't going to work. Any kind help is appreciated!
You might want to look at the automated testing via browser suites:
http://www.teknologika.com/blog/the-holy-grail-net-automated-web-gui-testing-for-internet-explorer/
http://watin.sourceforge.net/
I wrote a blog post on this awhile back: Web scraping in .NET. That discusses cookies but not JavaScript; I don't know if that would require additional coding.
Might be worth having a look at selenium .
We use it for web testing in a C# asp.net envirnorment.
The documentation isn't to bad

ASP.NET open new IE window and run "find" programatically?

Can I use Internet explorer's "find" to find a number (and go to the first result) in a webpage after I open a new IE window through ASP.NET?
Edit : Maybe I should clarify, I am opening a page on a site that is not mine, I cannot embed and run javascript on it...
Is this even possible?
Thanks
Roey
Not ASP or IE-specific: most sites use highlighting only, which you could also do on the client side using jQuery, like with the Text Highlight plugin.
You would then be missing the "Next" and "Previous" buttons, but I guess someone solved that problem already as well...
EDIT: As you clarified that the content is from some other site: this cannot be done unless you show the content from within your own URL (which is probably not accepted by the owner of the other site). Click for example a Google cache result (for which the content is served from a Google URL) which does do highlighting, while clicking a normal search result (which is served from the site's web server) doesn't do it. That's why Google offers a toolbar that allows for highlighting after all, and that's why people use bookmarklets.
One thing you can do is have your server request the page itself, and then modify the markup with something like jQuery as Arjan mentioned.
With ASP.NET, do something like:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.siteiwanttofindnumberon.com/pagetoopen.html");
request.Headers = new WebHeaderCollection();
//set up headers as necessary
request.Method = "GET";
//retrieve the response
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
b = new List<byte>();
while (b.Count < request.ContentLength)
b.Add((byte)response.GetResponseStream().ReadByte());
Now you have a List that represents the response stream, as though you'd sent the response yourself with a telnet client or a web browser. You can do as you please with this - for instance, injecting jQuery code to do highlighting for you.
Personally, I would manually scan this list for the information I want and wrap it in a span to highlight it. I would also try to put an in-page anchor at that point and redirect the target of the request to that anchor, thereby forcing the browser to scroll down to the highlighted text. Again, jQuery or another Javascript framework could accomplish this for you as well.
Finally, you'll want to find a way to render this stream to the client. I'm not sure off the top of my head if you can do this in a new window. You may need to manually construct an iFrame-modal-popup type thing, or use an HttpHandler.
Even if you had JavaScript access I don't think you could access the Ctrl-F functionality in a browser. Now that you don't even have JS, I guess the answer is no can do...
No, there's no JS-accessible mechanism to do this. There used to be a proprietary IE API for this called NavigateAndFind() but it was removed in IE7 or IE6SP2.
http://msdn.microsoft.com/en-us/library/ms536641(VS.85).aspx

Categories