SetAttribute for value isnt working correctly - c#

I've been trying to get SetAttribute with the webBrowser.Document for a while, and for some reason the webBrowser fields don't get filled in.
I'm certain that I have the ID's correct, but perhaps i'm just overlooking something. I am setting textfields for the netflix login website.
My code is as follows:
var emailField = webBrowser1.Document.GetElementById("email");
var passField = webBrowser1.Document.GetElementById("password");
emailField.SetAttribute("value", username);
passField.SetAttribute("value", password);
Thanks for any help, the username is a string which includes the email, and password contains the password, also in a string.

You should put your code in DocumentCompleted.
Example
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var emailField = webBrowser1.Document.GetElementById("email");
var passField = webBrowser1.Document.GetElementById("password");
emailField.SetAttribute("value", "123");
passField.SetAttribute("value", "456");
}
Try it again.

Related

Selenium entering invalid text (Half text) in text box

While running multiple test cases, selenium is entering invalid text as shown in the attachment.
Below are the two Examples showing the error:
Instead of entering AutomationTest123_6035633258972, it just enters 6035633258972. .
Instead of Entering AutomationTest123_636010703068635512, it just
enters utomationTest123_636010703068635512. .
Code
//StaticVariable is class which has static values
var username = StaticVariable.username;
var password = StaticVariable.password;
driver.FindElement(By.Id("username")).SendKeys(username); //putting wrong values
driver.FindElement(By.Id("password")).SendKeys(password); //putting wrong values
driver.FindElement(By.Id("login")).Click();
Can anyone help me on this? Any help would appreciated.
first of all make sure that
var username = StaticVariable.username;
is assigning the right value.
use a
print
to check what the value of username after assign.
than try by using the below code:
driver.FindElement(By.Id("username")).click()
driver.FindElement(By.Id("username")).clear()
driver.FindElement(By.Id("username")).SendKeys(username);
There can be a couple of reasons I've seen that this can be an issue. I'd first try to clear out the text input first and make sure it has focus.
var username = StaticVariable.username;
var password = StaticVariable.password;
// Fill out user name
var userElem = driver.findElement(By.id("username"));
userElem.click();
userElem.clear();
userElem.sendKeys(userName);
// Fill out password
var passElem = driver.findElement(By.id("password"));
passElem.click();
passElem.clear();
passElem.sendKeys(password);
// Click login button
driver.findElement(By.id("login"))
.click();
If, however, that doesn't fix it, I've had situations where splitting the string up and sending keys a few at a time has worked. That does have a performance penalty so I wouldn't do it unless you really really need to.
public void fooTest() {
// Do stuff to get to the correct page, etc
var username = StaticVariable.username;
var password = StaticVariable.password;
// Fill out user name
var userElem = driver.findElement(By.id("username"));
sendKeys(userElem, userName);
// Fill out password
var passElem = driver.findElement(By.id("password"));
sendKeys(passElem, password);
// Click login button
driver.findElement(By.id("login"))
.click();
// do assertions, etc
}
private void sendKeys(WebElement elem, String keys) {
elem.click();
elem.clear();
for (int i = 0; i < keys.length(); i) {
elem.sendKeys(keys.charAt(i));
}
}
Note : sorry for any syntax / c# errors, I've barely used the language ... I'm much more familiar with Java ;-)
Use the clear method then type the text: driver.findElement(By.cssSelector("").clear();
It may help.
Rather than using 'var' , you might want to try putting in the specific type , such as 'string'. The problem has to be in what is being passed to .SendKeys() or how it is being received by the function.
string username = StaticVariable.username;
string password = StaticVariable.password;

Get query string result without using response.redirect

protected void ConnectToFaceBook_btn_Click(object sender, EventArgs e)
{
string url = System.Uri.EscapeDataString("http://localhost:1298/FacebookChatApi/Default2.aspx");
string clientID = "507061112747022";
string redirecturl = #"https://graph.facebook.com/oauth/authorize?client_id="+clientID+"&redirect_uri="+url;
Response.Redirect(redirecturl);
GetAccessToken();
}
As you can see I am calling GetAccessToken() after Response.Redirect the given URL will return a query string called code and GetAccessToken. I am reading this query string but as I am doing it everything after Response.Redirect is not being executed. Is there any work around to call the redirecturl and get the code value without using Response.redirect?
there is a program in linux,php,python and etc called curl that act like a browser in your program, curl in C# have some alternative that u can use them curl in C# have some alternative that u can use them.

How do you get a User Identity?

Once a user is authenticated and authorized, the application can get information about the user by using the User object’s Identity property. The Identity property returns an object that includes the user name and role information.
Below is the code snippet which I have used to understand the concept:-
private void Page_Load(object sender, System.EventArgs e)
{
Label1.Text = User.Identity.IsAuthenticated.ToString();
Label2.Text = User.Identity.Name;
Label3.Text = User.Identity.AuthenticationType;
}
Is there any other way to get the User Identity?
Please clarify if you are wanting something like the following below..
If you just want the users Identity.. from an ASP.NET WebPage when the Page_Load is called create a string[] and do something like the following
string strRawUser = Page.User.Identity.Name;
Then from there strRawUser will have something like "DomainName\UserName"
So you need to Split the string into an stringArray and get the string[1] value like this
string[] strRawUserSplitter = Page.User.Identity.Name.Split("\\");
Label2.Text = strRawUserSplitter[1]
You can also get the current user identity using Principal Object.

Problems using captured access token to retrieve user's facebook information

I have been attempting to code a windows form application that interacts with facebook to retrieve the access token that has permissions to get some of the user's information. I have been trying to get the birthday of myself using the following code but it keeps giving me the 400 bad request error. Basically after running this code, and logging in at the authentication it is suppose to show a messagebox containing the user's birthday. In this case, I am using my own user id in the api.GET method. It seems to be the access token issue as when I don't pass in any tokens, i can view public available information such as id using the same code but I print out the access token to check and it seems to be alright. Any help would be much appreciated. First time posting here
public partial class AccessTokenRetrieval : Form
{
private string accessToken=null;
public AccessTokenRetrieval()
{
InitializeComponent();
}
private void accessTokenButton_Click(object sender, EventArgs e)
{
string getAccessTokenURL = #"https://graph.facebook.com/oauth/authorize?client_id=223055627757352&redirect_uri=http://www.facebook.com/connect/login_success.html&type=user_agent&display=popup&grant_type=client_credentials&scope=user_photos,offline_access";
getAccessTokenWebBrowser.Navigate(getAccessTokenURL);
}
private void getAccessTokenWebBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
string successUrl = #"http://www.facebook.com/connect/login_success.html";
string urlContainingUserAuthKey = e.Url.ToString();
MessageBox.Show(urlContainingUserAuthKey);
int searchInt = urlContainingUserAuthKey.IndexOf(successUrl);
MessageBox.Show(searchInt.ToString());
if (urlContainingUserAuthKey.IndexOf(successUrl) == -1)
{
string accessTokenString;
accessTokenString = Regex.Match(urlContainingUserAuthKey, "access_token=.*&").ToString();
this.accessToken = accessTokenString.Substring(13, accessTokenString.Length - 14);
//100001067570373
//MessageBox.Show(accessToken);
accessTokenTextBox.Text = this.accessToken;
Facebook.FacebookAPI api = new Facebook.FacebookAPI(this.accessToken);
JSONObject me = api.Get("/100001067570373");
MessageBox.Show(me.Dictionary["user_birthday"].String);
}
}
#
I would request you to try http://facebooksdk.codeplex.com and checkout the samples folder.
It includes sample for WinForms authentication and also making various request to Facebook.
Here are other useful links that I would recommend you to read.
http://blog.prabir.me/post/Facebook-CSharp-SDK-Writing-your-first-Facebook-Application.aspx
http://blog.prabir.me/post/Facebook-CSharp-SDK-Making-Requests.aspx

Getting only the Referrer page not complete address in .NET

I am using C#. Below is my sample code.
private void Page_Load(object sender, System.EventArgs e)
{
string str = Request.UrlReferrer.ToString();
Label1.Text = str;
}
The result in Label1.Text is http://localhost:82/data/WebForm1.aspx.
Now I want the result "WebForm1.aspx" in Label1.Text
can you please help me?
Thanks.
If you want only the part after the last / in the URL, calling the System.IO.Path.GetFileName() method on the Uri.LocalPath should do the trick:
System.IO.Path.GetFileName(Request.UrlReferrer.LocalPath);
If you want the output to keep query string information from the URI, use the PathAndQuery property:
System.IO.Path.GetFileName(Request.UrlReferrer.PathAndQuery);
Try the LocalPath property on the UrlReferrer:
Label1.Text = Request.UrlReferrer.LocalPath;
It should provide you with just the filename.
Edit: this seems to also include the path, so only works for root.
In which case, you're better off just using Substring():
string str = Request.UrlReferrer.ToString();
Label1.Text = str.Substring(str.LastIndexOf('/')+1);

Categories