1

I have button called 'sign in' and that button redirects to a sign in screen into a new tab where I need to enter username and password using selenium sendKeys method. But it keeps saying: 'no such element'. Please help me. What am I doing wrong?

driver.get("https://example.com"); 
driver.manage().window().maximize(); 

//This button redirecting to a new url into new tab
driver.findElement(By.className("click_sign_in_button")).click(); 

driver.findElement(By.id("username")).sendKeys("[email protected]");
driver.findElement(By.id("password")).sendKeys("1234");
driver.findElement(By.className("login")).click();

3 Answers 3

1

As it is opening up the URL in new tab, you should first switch driver focus to that and try to find element. Check driver.getWindowHandles's count, if it is more than 1, you can try below code.

driver.get("https://example.com"); 
driver.manage().window().maximize(); 
driver.findElement(By.className("click_sign_in_button")).click(); 
//code to switch focus
ArrayList<String> multiWindow = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(multiWindow.get(1));
driver.findElement(By.id("username")).sendKeys("[email protected]");
driver.findElement(By.id("password")).sendKeys("1234");
driver.findElement(By.className("login")).click();

If windowhandle count is 1, then you can try switching b/w tabs as

driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "2");
Sign up to request clarification or add additional context in comments.

Comments

0

Most likely you need to wait for the page to load. You can try adding a pause after clicking the button before you send the keys, or waiting specifically for the username element to be available.

1 Comment

I have used a wait command(Implicit wait) but i thing the problem is with URL is getting change and i don't know how to manage it
0

There may be one of the cause for the wait time.After clicking on sign in it will immediately search for username id.So you need to use implicit wait,explicit wait or simple wait commands like below.

Insert Thread.sleep(3000); //numbers are in ms. Insert above command at 3places after sign in prompt,after entering username prompt and after entering password prompt.

Try this mostly it will works👍

2 Comments

The problem is with the URL only as its getting change by redirecting it, because its locating the webelements without redirecting the url.Also i tried what you said added Thread.sleep method but it didn't work
Otherwise you can directly take the url which comes for appropriate sign in page and paste in on the driver.get()method instead of old url. So if you keep the direct sign in page url then you will get land on sign in page.so you can easily enters username and password by entering send keys in particular field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.