Page factory initilization issue - selenium

Does PageFactory.initElements(driver,this) try to initialize all element written in the page?
I have login page where only username and login button is there. once i enter username and hit login then password box comes in.
I have written username,password and Login button findby method in LoginPage class. Now before entering username and clicking on login button, i am trying to initilize the LoginPage. As the password field is not there will it throw error ?
Thanks,

I got the answer after doing lots of googling and debugging my page factory code initialization.
When we try to initialize the page using pagefactory init element it creates the proxy of each element and when we call webelement to do some actions on it like element.click() then it try to find the element in the page where you are right now.
One good post i found is at:
https://rationaleemotions.wordpress.com/2016/09/05/understanding-pagefactory/

Related

Selenium ChromeDriver shows empty page when loading site with credentials

I'm writing automated tests for a website that uses pop-up authentication. I am able to provide the credentials by using the username:password#https://site.address.com formula, but in that case the Chrome window's title bar simply shows data;: and a blank page. After this, my tests will fail as the page is not really loaded and the elements can't be clicked.
If I don't provide credentials, then the title bar will show my URL, but I can't enter the credentials to enter the page, as the pop-up window has no elements I can refer to.
EDIT: when I try to send in the authentication data using driver.switchTo().alert() then the execution basically stops and I have to press cancel.
controller.getNavigator().goToPage(loginSiteAddress);
System.out.println(userName);
System.out.println(password);
controller.getNavigator().authenticate(userName, password);
public void authenticate(String userName, String password) {
driver.switchTo().alert().sendKeys(userName + Keys.TAB + password);
driver.switchTo().alert().accept();
}
How can I make the 1st solution work?

How to sendKeys in textBox? for automation

I'm doing a test on c# with selenium for this site http://onliner.by.
At first I have to be authorized on this site.
I found(by xpath) button with name"Вход" in the right upper corner and click on it.
Then there's refreshed and page changed, but the link remained the same (http://onliner.by).
And I need to enter login and password on this page and sumbit it. But I cannot do it.
I founded Xpath paths of this elements and I used this code:
//this doesn't work
driver.FindElement(By.XPath("//*[#id='auth-container__forms']/div/div[2]/form/div[1]/div[1]/input")).SendKeys("user");
driver.FindElement(By.XPath("//*[#id='auth-container__forms']/div/div[2]/form/div[1]/div[2]/input")).SendKeys("password");
driver.FindElement(By.XPath("//*[#id='auth-container__forms']/div/div[2]/form/div[3]/div/button")).Click();
How can I do it? I tried to use SwitchTo().Frame but it didn't help too.
I will be very grateful for help.
You have to wait element availablity like this
new WebDriverWait(driver, TimeSpan.FromSeconds(timeOut)).Until(ExpectedConditions.ElementExists((Locator Value)));

cannot login with selenium

I am trying to automate logging into this website.
the login menu is at the top right of the website.
i have tried to find the login form by CSS selector, Xpath, name and the action of this topic.
edit 1 : I have also tried hover the mouse over the login with actionchains then find by elements. No Element was found, no key was sent also.
Result:
the find by CSS, Xpath, name shows : element not visible ;
the action event : i hover the mouse over the login area, send keys,the login area pops up but no key was sent, no error shown.
My code for the action event:
wd.get("http://www.vatgia.com")
action = ActionChains(wd);
action.move_to_element(wd.find_element_by_css_selector("#header_bar > div > div.fr > a:nth-child(8)"))
action.send_keys("loginsampling")
action.perform();
p/s : thankyou Brian for the edit !
Final update :
Mahipal has provided me with the solution. i should have used the xpath with regards to the container for the login form and password. i previously only right click and copy the xpath element and that did not work. Thanks Mahipal !
You can make use of the container of login menu for identifying its fields, as shown below:
driver.findElement(By.xpath("//div[#id='header_bar']//a[#rel='#header_login']")).click();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(
driver.findElement(By.xpath("//div[#id='simple_tip']//div/input[#name='loginname']"))));
driver.findElement(By.xpath("//div[#id='simple_tip']//div/input[#name='loginname']")).sendKeys("login_name");
Try above code and let me know, whether it works for you.
This is due to the fact that you are not opening the login dropdown prior to attempting to find the login button. You will need to find the Dropdown element, click on it, then find the login fields and button and perform your actions.

What will beselenium webdriver identifier for flipkart login button in java?

webdriver is throwing the element not found exception when trying to login to flipkart using webdriver.
need the valid identifier for flipkart login button
Navigate to http://www.flipkart.com/
CLick on login button on the top right hand corner
Notice the login button in the popup
I was facing the same problem while trying to automate the Flipkart login page in Chrome using selenium.
Sadly for me none of the online suggestions worked, I tried
1. GetWindowHandle way
2. Passing parameters directly to the page using http://user:pass#host.com
3. Tried to check for multiple frames
Finally what worked was, I copied the "Driver.FindElement & WebElement.Click" code twice, Basically the problem was the Login popup window was getting closed after the first click due to which the element was not found.
This seems to be a machine/version specific issue with chrome.
You can try this.
driver.get("http://www.flipkart.com/");
driver.findElement(By.linkText("Login")).click();
driver.findElement(By.id("login_email_id")).sendKeys("email_id");
driver.findElement(By.id("login_password")).sendKeys("password");
driver.findElement(By.cssSelector("input[value='Login'][type='submit']")).click();
Sample below:
//Click on 'Login'
driver.FindElement(By.Css(".js-login")).Click()
//Enter email
driver.FindElement(By.Css("#login_email_id")).sendKeys('emailvalue');
//Enter pswd
driver.FindElement(By.Css("#login_password")).sendKeys('password');
//press on login button
driver.FindElement(By.Css("#login-dialog .btn[value='Login']")).click();
Try the following:
WebDriver wb = new InternetExplorerDriver();
wb.get("http://www.flipkart.com/");
wb.findElement(By.xpath("//*#id='container']/div/div/header/div[2]/div/div[1]/ul/li[8]/a[contains(text(),'Log In')]")).click();
Try this:
driver.get("https://www.flipkart.com/");
driver.findElement(By.className("_2zrpKA")).sendKeys(" YOUR USER NAME");
driver.findElement(By.xpath("//input[#type=\"password\"]")).sendKeys("YOUR PASSWORD");
driver.findElement(By.className("_2zrpKA")).submit();

Login check using HtmlUnit

Hy... i want to login to some 3rd party sites using HtmlUnit. But HtmlUnit should be able to tell me whether the login attempt to the input site is successful or not. Is there any way around to perform this task using HtmlUnit. Please help ..!!!
Thanks
Usman Raza
I'm currently using HTMLunit to log in to a site that has a varification page and redirect. some of my code for this is:
//---------------------------------Login Page---------------------------------
HtmlPage PageLogin = webClient.getPage(url);
HtmlElement submitButton = (HtmlElement) PageLogin.getByXPath(Xpath To Button).get(0);
HtmlTextInput name = (HtmlTextInput) PageLogin.getElementById("UserIdInput");
HtmlPasswordInput pass = (HtmlPasswordInput)PageLogin.getElementById("ADloginPasswordInput");
name.setText(username);
pass.setText(password);
System.out.println("Logging in to site");
//------------------------------------------------------------------------
//---------------------------------Pass varified Page----------------------
HtmlPage pagePassVarified = submitButton.click();
System.out.println("Successfully Logged in to site");
HtmlElement btnContinue = (HtmlElement) pagePassVarified.getElementById("BtnClickToContinue");
//---------------------------------------------------------
//---------------------Home Page----------------------------------
HtmlPage pageHome = btnContinue.click();
System.out.println("Home Page accessed");
//----------------------------------------------------------------
This code goes to a login page, adds username and passwords to text boxes, and clicks the submit button. We are next redirected to a "wait 5 seconds, or click here to continue to home page" type of page, where the continue button is clicked. Lastly we arrive at our home page that we wanted to log into. I selected the page elements by both ID and Xpath when no ID was available.
You can have HtmlUnit check the URL, or search for a specific element on the page, more precisely one you know to be present only in one case (sucessful login / rejected).
Like RabidFX suggested, I would suggest you to check URL but in some cases (I've seen such situations in my work experience) URL may still the same. Then, my suggestion would be checking for a specific element and it should be the login form because generally unsuccessfull login attemps redirect you to the same page that has the same login form. That would not be hard - coded solution because I hope you have found some way to get that login form with a generic way :)