How to find elements in DYNAMIC page using selenium - java? - java

I'm new to selenium web-driver and i'm trying to make a simple login automation.
Here is the link to the ebay login page: https://signin.ebay.com/
Here is the html code for the username input box:
<input size="40" maxlength="64" name="2045849967" id="2045849967" type="text" autocapitalize="off" autocorrect="off" placeholder="Email or username" class="fld">
Here is the html code for the password input box:
<input size="40" maxlength="64" name="955270437" id="955270437" type="password" autocapitalize="off" autocorrect="off" placeholder="Password" class="fld">
Here is my code trying to sendKeys into the username and password input boxes:
driver.findElement(By.id("2045849967")).sendKeys("blabla#gmail.com");
driver.findElement(By.id("955270437")).sendKeys("somePassword");
The problem is that I can't find the elements by these ids since it's change every time the page is loaded/refreshed.
Please note that the all id is completely change.
What is my options to solve the problem?
Thanks
UPDATE:
I succeed to get into the username box by finding the 'register' element in the login page and clicking 'Tab', but now when i'm into the username box I don't know how can I 'sendKeys' into because I don't really have the username element.
Here is the code:
WebElement regElement = driver.findElement(By.id("regTab"));
regElement.sendKeys(Keys.TAB);
Now how can I continue from here ?
I need to insert the username and then click 'Tab' again and insert password.
What is the options?

You can use these
By.cssSelector("input[placeholder='Email or username']")
By.cssSelector("input[placeholder='Password']")
Apparently, there are two matches for the email field and the first one is hidden which causes this to fail. You can still use these selectors, you'll just have to grab the 2nd match for the first locator.
driver.findElements(By.cssSelector("input[placeholder='Email or username']")).get(1).sendKeys("abc");
driver.findElement(By.cssSelector("input[placeholder='Password']")).sendKeys("abc");

I found a solution for this problem.
WebElement regElement = driver.findElement(By.id("regTab"));
regElement.sendKeys(Keys.TAB);
WebElement currentElement = driver.switchTo().activeElement();
currentElement.sendKeys("blabla#gmail.com");
currentElement.sendKeys(Keys.TAB);
currentElement = driver.switchTo().activeElement();
currentElement.sendKeys("somePassword");
driver.findElement(By.id("sgnBt")).click();

Related

How can I get Selenium WebDriver (Java) to click this button?

I am new at using Selenium WebDriver to automate test cases. So far (using Selenium and Java), I am able to open the testing website, enter the username and password, and log in. After logging in, however, the user is re-directed to a screen with a security warning that must be accepted before they can access the actual website. To do this, they must click on a button called "I Agree". I can't get Selenium to click the button and, without it, I can't get to the rest of the site to automate. Here is the HTML for the button:
<form name="landingHandlerSF" method="post" action="/apps/bap/secLandingHandler.do">
<input name="userAgreedTerms" value="" type="hidden">
<input name="submit" value="landing" type="hidden">
<input name="buttonAction" value="I Agree" onclick="setValue('agreetoTerms', 'Y')" type="submit">
</form>
Here is the code I have tried (which doesn't work):
WebElement button = driver.findElement(By.name("buttonAction"));
button.click();
Could someone please help me with this?
As per my understanding, page is redirecting to new html page but driver will be pointing to parent page(Login page in your case) , so you may have to switch to child window in order to click on I Agree button.
The following code will switch the driver away from the current window (ie, the login window) to the new window (ie, the security warning). After clicking I Agree that security warning will be closed and the driver will switch back automatically
String thisWindow = driver.getWindowHandle();
Set<String> windowHandles = driver.getWindowHandles();
for (String windowHandle : windowHandles) {
if (!windowHandle.contains(thisWindow)) {
driver.switchTo().window(windowHandle);
}
}
If it is not navigating to new page then it must be under some iframes , in that case you may need to switch to frame and click the button.
Hope this will work.
Try this and let me know what happened.
Did you try to inject JavaScript code into the browser?
driver.executeScript("setValue('agreetoTerms', 'Y')");
or
driver.executeScript("document.getElementsByName('buttonAction')[0].click()");

Unable to locate element for password field of Gmail login page

I am trying to write a program for Gmail login using Selenium. I am able to hit URL, enter username and click "Next" button, but when I redirected to "Password" field page I am unable to locate that element. I tried the same using by.className. Following is the inspected code for field:
<input class="whsOnd zHQkBf"
jsname="YPqjbf"
autocomplete="current-password"
spellcheck="false"
tabindex="0"
aria-label="Enter your password"
name="password"
autocapitalize="off"
autocorrect="off"
dir="ltr"
data-initial-dir="ltr"
data-initial-value=""
badinput="false"
type="password">
and following is selenium code I have written:
driver.findElement(By.className("whsOnd zHQkBf")).sendKeys("Password");
I also tried using Id, by using parent <div id ="password"....>, but it didn't work.
Please let me know the solution.
whsOnd zHQkBf means that the element has two CSS classes whsOnd and zHQkB. So you won't find anything that has a class with the name whsOnd zHQkBf.
You have to instead use a CSS selector to find an element with multiple classes like this: .whsOnd.zHQkBf. But this matches multiple elements on the google login page. It's easier to use the following CSS selector input[type='password'].
Please use the below code
driver.findElement(By.cssSelector("input[type='password']")).sendKeys("Password");
use the following code
driver.findElement(By.cssSelector("[name=\"password\"]")).sendKeys("Password");
The name locator would be the simplest :
driver.findElement(By.name("password")).sendKeys("Password");
This might help you:
driver.findElement(By.xpath(".//*[#id='password']//input[#name='password']")).sendKeys("Password");
driver.findElement(By.className("whsOnd zHQkBf")).sendKeys("Password");
Hi buddy,
The statement which you have written is wrong class name because its a compound class and we cannot use compound class to locate an element. The username and password fields are developed using angular-JS so when click sign in url both username and password will be downloaded but for user only username is shown so the password is hidden background
Try this code this will work for sure because i have tried already
driver.findElement(By.xpath("//[#id='password']/child::[1]/child::/child::/child::*")).sendKeys("Password");
Hope you got answer
Happy Testing
Below code worked for me. Some time element will not be clickable so make driver to wait for some expected events to happen like clickable.
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[type=password));
driver.findElement(By.cssSelector("input[type=password]")).sendKeys("your password");

Dropdown based on user input

Guys could someone help me to find solution for following case:
I have to type several letters for username (actually typed whole username) and click on it to choose. It looks as on screenshot which I added below.
Here is how it looks like on a page:
Added part
<div class="show-temp">
<div class="medium-12 column">
<div id="suggestionBox" class="search-input">
<label for="viewUser">Search user names</label>
<input type="text" autocomplete="off" id="viewUser" onkeydown="javascript: if(event.keyCode == 13 ){ userStats.HideContent();userStats.ApplyFilters(this); return false;}" placeholder="Search user names..">
I tried with this code:
Select dropdown2 = new Select(driver.findElement(By.xpath("//*[#id=\"viewUser\"]")));
dropdown2.selectByVisibleText("Zoran21");
Thread.sleep(2000);
And I got this error:
Element should have been "select" but was "input"
Please assist and thank you in advance
The Select class only works with <select> tags ,it's does not work for other Tags LIke (Input,div).if you want to work with DropDown first check the DoM ,how it's build .if its contain <select> Tag then Use Select Class other wise following the Below Approach.
The Error Indicates to you , DropDown Build With <Input> Tag so Your unable to Select Options By using Select Class
use below approach may be it's help you.
String searchUserXpath ="....";
String optionXpath ="----"
driver.findElement(By.xpath(searchUserXpath )).sendKeys(entersearchOptionValue totally /partially );
//for example option value is HYderabad ,you HYd Then takes xpath of
// HyderabaOPtion...
driver.findElement(By.xpath(optionXpath )).click();

Finding web element using Java/selenium webdriver

HTML CODE
input class="btn primary" onclick="return login_jsp.saveRemember && login_jsp.saveRemember() || true;"
type="submit" value="Sign In"
Following approaches did not help :
1)
driver.findElement(By.className("btn primary")).sendKeys(Keys.ENTER).click();
2)
driver.findElement(By.cssSelector("input[class='btn primary']")).click();
(OR)
1)
driver.findElement(By.className("btn primary")).sendKeys(Keys.ENTER).sendKeys(Keys.ENTER);
2)
driver.findElement(By.cssSelector("input[class='btn primary']")).sendKeys(Keys.ENTER);
Please suggest.
you can try like this:
driver.findElement(By.cssSelector("input.btn.primary")).click()
See Sendkeys and Click won't work together.
So if there is some text field you can enter value there by :
driver.findElement(By.className("btn primary")).sendKeys(Keys.ENTER);
and then you if need to click some button or link you can use Click() as:
driver.findElement(By.cssSelector("input[class='btn primary']")).click();
Note: You should take the xpath of the same, to perform either sendkeys() or click()
Reply back to me if you have further query.
Happy learning:-)
You can try with below locators
Css Selector
input[value='Sign In']
input.btn.primary
xpath
//input[#value='Sign In']
//input[#class='btn primary']
please make use that above ones fetching specific element or not, may be by using firepath before using in webdriver script.
If you want to simulate ENTER then go for sendkeys with key events. If you want to just click on element then just use click();
this link helps you to write css selectors and this link you on xpath
Thank You,
Murali
input class="btn primary" onclick="return login_jsp.saveRemember && login_jsp.saveRemember() || true;"
type="submit" value="Sign In"
try below approches
1. driver.findElement(By.cssSelector(".btn primary")).click();
2. driver.findElement(By.xpath("//input[#type='submit']").click();
3. driver.findElement(By.xpath("//input[#value='Sign In']").click();
3. driver.findElement(By.xpath("//input[text()='Sign In']").click();

How to identify an element in selenium even if xpath is same

I am new to selenium and trying to autoamte a web application in junit framework. As many get some problem in identifying web elements,I too stuck at a point where two submit buttons are having same xpath and css selector.
The only difference what I can observe is.. In the two form tags, I can see that className is different(for first form tag it is "feature_space_checkbox" and for second form tag it is "auto_fs_steps_checkbox")
As, I need to identify the second submit button..So I tried to identify the second submit button as below
driver.findElement(new ByChained(By.className("auto_fs_steps_checkbox"),By.xpath("//*[#id='edit_brochure_2863']/input[3]")));
When I try to execute this, I got the error as
org.openqa.selenium.NoSuchElementException: Cannot locate an element using By.chained({By.className: auto_fs_steps_checkbox,By.xpath: //*[#id='edit_brochure_2863']/input[3]})
Can anyone please correct me where I made the mistake
Adding DOM for this scenario
<form action="/brochures/2865/feature_space_checked" class="feature_space_checkbox" id="edit_brochure_2865" method="post"><div style="margin:0;padding:0">
<input name="commit" type="submit" value="Submit">
</form>
For the second submit button it is..
<form action="/brochures/2865/update_auto_fs_steps" class="auto_fs_steps_checkbox" id="edit_brochure_2865" method="post"><div style="margin:0;padding:0">
<input name="commit" type="submit" value="Submit">
</form>
Firstly, XPath and CSS selectors are not definitive. There are many XPath and CSS for every element on a page so to say they have the same Xpath and CSS selectors is incorrect.
For your example, is there any need to use XPath or combine two selectors?
The following CSS would work;
form.auto_fs_steps_checkbox input
There is no need to use chaining as this can all be expressed in XPath:
//*[#id='edit_brochure_2863' and #class='feature_space_checkbox']/input
So this will be in Java:
driver.findElement(By.xpath("//*[#id='edit_brochure_2863' and #class='feature_space_checkbox']/input"));
Of course, for the second submit button it will be
driver.findElement(By.xpath("//*[#id='edit_brochure_2863' and #class='auto_fs_steps_checkbox']/input"));
xpath for the second submit would be
driver.findElement(By.xpath("//form[#class='auto_fs_steps_checkbox']/input"));
This is enough to identify the second button as here class name is unique and id is same for both. So its better we do it by class name .

Categories