I am trying to automate a web page using selenium WebDriver. I can move towards the webpages and can able to do all tasks on parent pages. But at one place I am getting a POPUP window which asks username and password. I am unable to identify the username and password text box id's, how can I find the element (TextBox) and send the username and password through selenium webDriver code.
You can try this by using your web URL,
#Test
public void testlinkedin() throws Exception {
driver.get("https://www.linkedin.com/"); //Use your web URL
driver.findElement(By.linkText("Sign Up")).click();
driver.findElement(By.cssSelector("button.fb-btn")).click();
Thread.sleep(3000);
Set <String>handles = driver.getWindowHandles();//To handle multiple windows
firstWinHandle = driver.getWindowHandle();
handles.remove(firstWinHandle);
String winHandle=handles.iterator().next();
if (winHandle!=firstWinHandle){
secondWinHandle=winHandle;
driver.switchTo().window(secondWinHandle); //Switch to popup window
Thread.sleep(2000);
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys("Username");
driver.findElement(By.id("pass")).clear();
driver.findElement(By.id("pass")).sendKeys("Password");
driver.findElement(By.id("u_0_2")).click();
driver.findElement(By.name("__CONFIRM__")).click();}
Make sure to declare these string variables as,
public String firstWinHandle;
public String secondWinHandle;
You need to switch to the new pop-up window to select its elements. Here is an example to switch to the new window:
String handle = driver.getWindowHandles().toArray()[1]; // 1 stands for the new window and 0 for the parent window
driver.switchTo(handle);
Now you can select your elements present in this window.
*Note: * To get back to original window you again need to switch back to it with 0 as the index.
Maybe the popup window is HTTP basic authenticated site. If so, then you cannot use the window handles as stated in previous answer, but in that case you have to send the username and password directly to URL request:
driver.get("http://username:password#your-test-site.com");
Its working fine for me as I used like this...
String handle = driver.getWindowHandles().toArray()[1].toString();
driver.switchTo().window(handle);
Related
I m trying write a code to access Aliexpress and search for an item, then extract the details, such as, Product name, price, etc.; on page by page to an Excel document. I seek through previous questions posted here to build it. Thanks to that.
Somehow I was able to search the item for first 5 or 6 test runs but then suddenly, Aliexpress asked me to either login or register.
1.) First question, Why any browser won't access the website without registering? Did they recognized my user-agent?
2.) Secondly, Then I was wrote a code to auto log in. Site contains lots of Javascripts, an it is an responsive site. Some html elements appear as we click them. When in the auto log in, my code won't detect the either E-mail or password elements of the page. Is there something preventing it from been detected? How can I solve this?
I here put my sample code:
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
//To input the user's search
Scanner nw1 = new Scanner(System.in);
System.out.println("What do you want to search?");
String a = nw1.nextLine();
//Open the driver
System.setProperty("webdriver.chrome.driver",
"E:\\JetBrains\\webdriver\\chrome\\chromedriver.exe");
WebDriver AE = new ChromeDriver();
//Open the web page and Login in.
AE.get("https://www.aliexpress.com/");
Thread.sleep(2000);
//xpath of account button
AE.findElement(By.xpath("//*[#id="nav-user-account"]/div/div/p[3]/a[2]")).click();
//xpath of Sign in button
AE.findElement(By.xpath("/html/body/div[9]/a")).click();
AE.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//xpath of Email box
AE.findElement(By.xpath("//*[#id=\"fm-login-id\"]")).sendKeys("my-email");
//xpath of password section to type
AE.findElement(By.xpath("//*[#id=\'fm-login-password\']")).sendKeys("my-password");
AE.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// xpath of submit button
AE.findElement(By.xpath("//*[#id="login-form"]/div[5]/button")).click()
Sry kind of my first time here.
Any helpful comments are welcome. Thanks.
It is because following x path in not clicking on account button instead it is clicking on sign in button
AE.findElement(By.xpath("//*[#id="nav-user-account"]/div/div/p[3]/a[2]")).click();
find element through id. Id is available to click on account
"nav-user-account"
while clicking verify that it is unfold through following class
"ng-item nav-pinfo-item nav-user-account user-account-unfold"
if unfold contain than box is open otherwise it is close. If close than click on it.
Try this one first.
Try applying delete cookies ,some sites access data from cache /cookies ,clearing it would solve.Also quit driver at the end of your script and open a new instance i.e driver.manage().deleteAllCookies(); and driver.quit();
xpath in your code needs to be corrected as AE.findElement(By.xpath("//*[#id='nav-user-account']/div/div/p[3]/a[2]")).click();
Use single quotes inside of your xpaths.
I'm trying to write a simple java based selenium code where I would load a page, give the desired values to username & password.
Once the page loads username is already focused but I can enter values into username or password
Using MAC - Eclipse
I am new to coding sorry so any help would be greatfully received
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.get("http://dc1.racbusinessclub.co.uk/login/");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Thread.sleep(2000);
driver.findElement(By.tagName("body")).sendKeys("rac-dc");
Thread.sleep(2000);
driver.findElement(By.tagName("body")).sendKeys(Keys.TAB);
Thread.sleep(2000);
Below is basic way of doing it.
driver.findElement(By.id("username")).sendKeys(username);
driver.findElement(By.id("password")).sendKeys(password);
pause(3);
WebElement button = driver.findElement(By.tagName("button"));
button.sendKeys(Keys.RETURN);
And once logged-in you can assert the url like below
Assert.assertTrue(driver.getCurrentUrl().endsWith("/yourURL"));
You can get username and password id from the browser by Right click on the page and select Inspect.
Even if the field is already focused, you must specify which element you wish to "send the keys" to. So grab the id of the username field, and then use sendKeys, like so:
driver.findElement(By.id("userNameInputBox")).sendKeys("rac-dc");
Also, worth noting that using Thread.sleep in your scripts is bad practice. If there's something you're waiting for, check for the presence of that element, rather than waiting an arbitrary amount of time.
Edit: Actually, seeing your response above I can see you're trying to interact with an alert popup, which you can do with:
driver.switchTo().alert().sendKeys(yourString);
Alternatively, you can skip that entirely and navigate to the page with your credentials in the URL - remember not to hard-code these:
driver.get("http://username:password#http://dc1.racbusinessclub.co.uk/");
I have been trying to create code to automatically log in into the website of a game I play called Life is Feudal to gather some information(Everything is legal, I have manually access to the informations I want but it would take years to gather everything).
So here's my code:
public static void main(String[] args) throws InterruptedException {
String Url = "https://lifeisfeudal.com/";
String email = "MYEMAIL";
String password = "MYPASSWORD";
WebDriver driver = setDriver();
// Open Life is feudal home page
driver.get(Url);
/*
* Logging into my account
*/
// Click on sign in button [WORK]
driver.findElement(By.linkText("Sign In")).click();
//Focus the Iframe
driver.switchTo().frame(driver.findElement(By.id("signin")));
/*
* Type in Email
*/
//Try #1 using name (DONT WORK)
driver.findElement(By.name("email")).sendKeys(email);
//Try #2 using xpath (DONT WORK)
driver.findElement(By.xpath("//*[#id=\"react-view\"]/div/div/div/div[2]/form/div[1]/div/input")).sendKeys(email);
/*
* Type in Password
*/
//Try #1 using name (DONT WORK)
driver.findElement(By.name("password")).sendKeys(password);
//Try #2 using xpath (DONT WORK)
driver.findElement(By.xpath("//*[#id=\"react-view\"]/div/div/div/div[2]/form/div[2]/div/input")).sendKeys(password);
/*
* Click the sign in button
*/
//Try (Havent tried it yet)
driver.findElement(By.linkText("Sign in")).click();
//Bring back to default
driver.switchTo().defaultContent();
//Rest of the code
}
And here's the html code inside the Iframe
HTML Iframe
Edit 1: Adding a thread.sleep(3000) after switching frame as suggested by Frank made it work.
I assume it wasnt fully loading the frame and was trying to access something not fully loading that wasnt existing at the time.
Add a Thread.Sleep(3000) after switching to the IFrame to figure out if it's a matter of timing. If so replace the Thread.Sleep(3000) with a more robust way of waiting like Explicit Wait.
Is it possible in Selenium Webdriver to pause the code execution using webdriver.wait until the user clicks the login button for a form?
The form contains a Captcha that the user has to manually fill in, so I can't automatically click the button using the script.
The login button on click returns the value of a JavaScript function in the form of Boolean value i.e either true or false.
Any possible solution for this problem?
Edit: completely new response after getting more info.
With the code below, this is what happens:
Selenium enters the user data
I then manually refresh the page (because I don't have login data to test)
Selenium enters the user data again
(the variable W is an earlier defined WebDriverWait.)
driver.Navigate().GoToUrl("https://www.irctc.co.in/eticketing/loginHome.jsf");
// will try looping as long as you're on the relevant page
do
{
try
{
IWebElement username = w.Until(ExpectedConditions.ElementIsVisible(By.Id("usernameId")));
IWebElement password = driver.FindElement(By.Name("j_password"));
if (String.IsNullOrEmpty(username.GetAttribute("value")))
{
username.SendKeys("a");
password.SendKeys("b");
}
}
catch (Exception)
{
// page is reloading, just wait another round
}
} while (!String.Equals(driver.Url, "put url after login here"));
I am trying to automate initial configuration(of my server) through webpage. After hitting my server ip https:/localhost:4443 and entering my credentials ,i get a window to change password(overlay/popup window).
Problem:- if i browse the same ip from another m/c or from another browser , i get a window over window i.e one more window over change password window(Please click the link to see the screenshot).
What i tried is to get the handle of the window but its not working, its providing one handle only.
**Its not frame also.
HTML code -- https://dl.dropboxusercontent.com/u/91420517/Html_Code.JPG
Here's my code
WebDriver driver=new FirefoxDriver();
driver.get("https://localhost:4443/ControlPoint/");
driver.findElement(By.xpath("//*[#id='name']")).sendKeys("xxxxxx");
driver.findElement(By.xpath("//*[#id='pass']")).sendKeys("xxxxxx");
driver.findElement(By.xpath("//*[#id='loginForm123']/div[6]/div[1]/div")).click();
Set<String> winIds = driver.getWindowHandles();
System.out.println("Total Windows --- " + winIds.size()); // its resulting the size as 1 which is not correct.
Iterator<String> it = winIds.iterator();
String mainWin=it.next();
String changeWin=it.next();
String shareWin =it.next();
driver.switchTo().window(shareWin);
String warning = driver.findElement(By.xpath("html/body/div[4234]/div[1]/span")).getText(); // to get the text on 3 window
System.out.println(warning);
How to resolve this issue .Please help. Any other way to click on buttons on window 3.
If the additional window is opened asynchronously, then possibly you are checking for it (with getWindowHandles()) too early, before it has been created - this is a common issue with Selenium tests and asynchronous page updates.
If this is the issue, it can be solved by trying a few times with a wait in between, checking each time whether a new window has appeared.