I have an MVC application that loads a video content package (from an external third-party vendor) into an iFrame. Under normal circumstances, when you make the call to this third party to retrieve the content, you pass it a ReturnURL parameter that tells the vendor's player where to redirect after the video content module has completed. Unfortunately, some new updated packages do not support the redirect. Instead of redirecting to the designated Redirect URL, upon the content module completing, it just redirects the iFrame to a page called "goodbye.htm" (which is still on the same domain as the third-party vendor).
Per recommendation, the vendor suggested we have a listener on the iFrame to detect when the iFrame src changes to this "goodbye.htm" page and then execute the code we need to redirect.
One of the problems we've encountered is getting the src of the iFrame AFTER it changes. Obviously just getting the src attribute from the iFrame only returns the original src, even after it changes. The other issue of course is that the domain of the goodbye.htm page is different than the domain of our application and the parent window, so it violates Same-Origin Policy.
Below is our JQuery function we're using to attempt the redirect.
$('.js__iframe-play').on('load', function () {
var src = $(this).attr("src");
if (src.includes("goodbye.htm")) {
const url = "/Learn/GetResults/" + $(this).data("class-id") + "?regid=" + $(this).data("registration-id");
console.log(url);
const newiframe = $(this).clone();
newiframe.attr("src", url);
$(this).after(newiframe);
$(this).remove();
}
})
Does anyone have any idea of how we can accomplish our goal?
I have developed a login page for a tracker. I have verified the username and password by validating the same against the DB.
When the validation is successful, I have just set the session variable with username as it is required throughout the session. But the session variable changes when it is redirected to the next page.
When retrieving the username in the next page, the session variable has been changed. The code is pasted below. Please help.
Code in Login page:
if (isValidUser)
{
Session["LoginUserName"] = Name;
Response.Redirect("xxx.aspx",false);
}
Code in xxx.aspx:
User= Session["LoginUserName"].ToString();
//Getting different value while retrieving User from session.
If you specify endResponse: false, then processing of the page & any events continues despite the Redirect which was added to the output stream. This could well change the Session.
Try calling Redirect without the endResponse: false parameter:
Response.Redirect("xxx.aspx");
If you specify endResponse: true or if you leave it out entirely, then processing of the page is silently aborted after the Redirect with a harmless ThreadAbortException. AFAIK this is the normal way of doing a Redirect, and you need to have good reasons to NOT abort after a Redirect, also you need to take great care through all your remaining code because page processing continues.
I'm having trouble removing/adding cookies in Selenium. I'm using Windows 7 and FireFox 25.0.1. My code looks like this:
Instance = new FirefoxDriver();
Instance.Manage().Window.Maximize();
var _cookies = Instance.Manage().Cookies.AllCookies;
Instance.Manage().Cookies.DeleteAllCookies();
foreach(Cookie cookie in _cookies)
{
Instance.Manage().Cookies.AddCookie(cookie);
}
var _newCookies = Instance.Manage().Cookies.AllCookies; //boom
On that last line I get the exception "Unexpected problem getting cookies." I've tried several variants of the above code and the same problem occurs the second time I call AllCookies-- even after closing and reopening the browser and calling GoToUrl(mysite) and re-adding the cookies (The browser was on mysite when I saved the cookies).
I've checked the cookies collection before accessing it, and they all have name/value pairs.
Has anyone managed to use the cookie API successfully in Selenium for C# or can say what I'm doing wrong?
You can only add cookies if your browser is displaying a page of the domain you want to drop the cookie on.
you don't appear to have navigated to a URL before dropping cookies.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am doing a project in which I have to make a windows application that can Take a URL in textbox from user. Now when the user press the Proceed button, the application should open that URl in a webbrowser control and fill the form on that page containing userID & password textboxes and submit it via the login button on that web page. Now my application should show the next page in that webbrowser control to the user.
I can open the url in the application's webbrowser control through my C# Code, but I can't figure it out that how to find the userID & pasword textboxes on that web page that is currently opened in the webbrowser control of my application, how to fill them, how to find the login button & how to click it through my C# Code.
For this you will have to look into the page source of the 3rd party site and find the id of the username, password textbox and submit button. (If you provide a link I would check it for you). Then use this code:
//add a reference to Microsoft.mshtml in solution explorer
using mshtml;
private SHDocVw.WebBrowser_V1 Web_V1;
Form1_Load()
{
Web_V1 = (SHDocVw.WebBrowser_V1)webBrowser1.ActiveXInstance;
}
webBrowser1_Document_Complete()
{
if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{
if (webBrowser1.Url.ToString() == "YourLoginSite.Com")
{
try
{
HTMLDocument pass = new HTMLDocument();
pass = (HTMLDocument)Web_V1.Document;
HTMLInputElement passBox = (HTMLInputElement)pass.all.item("PassIDThatyoufoundinsource", 0);
passBox.value = "YourPassword";
HTMLDocument log = new HTMLDocument();
log = (HTMLDocument)Web_V1.Document;
HTMLInputElement logBox = (HTMLInputElement)log.all.item("loginidfrompagesource", 0);
logBox.value = "yourlogin";
HTMLInputElement submit = (HTMLInputElement)pass.all.item("SubmitButtonIDFromPageSource", 0);
submit.click();
}
catch { }
}
}
}
I would use Selenium as opposed to the WebBrowser control.
It has an excellent C# library, and this kind of thing is the main reason it was developed.
You don't have to simulate filling in the username/password fields nor clicking on the login button. You need to simulate the browser rather than the user.
Read the login page html and parse it to find the ids of the username and password fields. The username can be obtained by looking for tags with name set as "username", "user", "login", etc. The password will usually be an tag with type="password". Javascript based popup panels for login would involve parsing the js.
Then follow the example code shown here, How do you programmatically fill in a form and 'POST' a web page?
The important thing here is that you're simulating a browser POST event. Don't worry about text boxes and other visual form elements, your goal is to generate a HTTP POST request with the appropriate key-value pairs.
Your first step is to look through the HTML of the page you're pretend to be and figure out the names of the user id and password form elements. So, let's say for example that they're called "txtUsername" and "txtPassword" respectively, then the post arguments that the browser (or user-agent) will be sending up in its POST request will besomething like:
txtUsername=fflintstone&txtPassword=ilikerocks
As a background to this, you might like to do a little research on how HTTP works. But I'll leave that to you.
The other important thing is to figure out what URL it posts this login request to. Normally, this is whatever appears in the address bar of the browser when you log in, but it may be something else. You'll need to check the action attribute of the form element so see where it goes.
It may be useful to download a copy of Fiddler2. Yes, weird name, but it's a great web debugging tool that basically acts as a proxy and captures everything going between the browser and the remote host. Once you figure out how to use it, you can then pull apart each request-response to see what's happening. It'll give you the URL being called, the type of the request (usually GET or POST), the request arguments, and the full text of the response.
Now, you want to build your app. You need to build logic which make the correct HTTP requests, pass in the form arguments, and get back the results. Luckily, the System.Net.HttpWebRequest class will help you do just that.
Let's say the login page is at www.hello.org/login.aspx and it expects you to POST the login arguments. So your code might look something like this (obviously, this is very simplified):
Imports System.IO
Imports System.Net
Imports System.Web
Dim uri As String = "http://www.hello.org/login.aspx"
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(uri), HttpWebRequest)
request.Timeout = 10000 ' 10 seconds
request.UserAgent = "FlintstoneFetcher/1.0" ' or whatever
request.Accept = "text/*"
request.Headers.Add("Accept-Language", "en")
request.Method = "POST"
Dim data As Byte() = New ASCIIEncoding().GetBytes("txtUsername=fflintstone&txtPassword=ilikerocks")
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = data.Length
Dim postStream As Stream = request.GetRequestStream()
postStream.Write(data, 0, data.Length)
postStream.Close()
Dim webResponse As HttpWebResponse
webResponse = DirectCast(request.GetResponse(), HttpWebResponse)
Dim streamReader As StreamReader = New StreamReader(webResponse.GetResponseStream(), Encoding.GetEncoding(1252))
Dim response As String = streamReader.ReadToEnd()
streamReader.Close()
webResponse.Close()
The response string now contains the full response text from the remote host, and that host should consider you logged in. You may need to do a little extra work if the remote host is trying to set cookies (you'll need to return those cookies). Alternatively, if it expects you to pass integrated authentication on successive pages, you'll need to add credentials to your successive requests, something like:
request.Credentials = New NetworkCredential(theUsername, thePassword)
That should be enough information to get cracking. I would recommend that you modularise your logic for working with HTTP into a class of its own. I've implemented a complex solution that logs into a certain website, navigates to a pre-determined page, parses the html and looks for a daily file to be downloaded in the "invox" and if it exists then downloads it. I set this up as a batch process which runs each morning, saving someone having to do this manually. Hopefully, my experience will benefit you!
So, the scenario is to login to said website using a formatted url, rather than doing something like what is below. Upon reading documentation regarding Selenium etc i read that this is bad practice (Excluding a login test case). I understand that the method i am inquiring about is not to be used on production level.
Does the url method pull the login information from a database?
//ENTER AGENT LOGIN ID
IWebElement LoadAgentLoginID2 = selenium.FindElement(By.Id("MainContent_txtAgentID"));
LoadAgentLoginID2.Click();
LoadAgentLoginID2.SendKeys(AgentLoginIDs[0]);
//ENTER ARC NUMBER
IWebElement LoadARCnumber2 = selenium.FindElement(By.Id("MainContent_txtArcNbr"));
LoadARCnumber2.Click();
LoadARCnumber2.SendKeys(System.String.Format("{0}", ARCnumbers[1]));
//ENTER PASSWORD
IWebElement LoadPassword2 = selenium.FindElement(By.Id("MainContent_txtPassword"));
LoadPassword2.Click();
LoadPassword2.SendKeys(AgentPasswords[0]);
Any help is greatly appreciated. Cheers!
The url method needs to be supported by the web application. If this is supported, then it would simply be something like this:
driver.get("http://www.mywebapp.com?username=JohnP&password=ABCD");
If the web app does not support this, then you should just try to encapsulate your login logic above into a separate and reusable method.