Dropdown based on user input - java

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();

Related

Clicking on Image Link with Selenium webdriver java

I have looked at the answers to similar questions and it seems like the code that I have should work but I get "Cannot click on element" error when the code invokes click on the web element.
Following is html markup segment
<div class="x-tree-node-item">
<a title="Manage Users" class="sidenavmenu_unselected" id="m-22" onclick="toggleMenu('22', '');" href="#">
<img title="" align="bottom" id="mi-22" alt="" src="ca/images/arrow.png" border="0">Manage Users
</a>
<div style="margin-left: 1em;">
<ul class="submenu-show" id="mp-22" style="height: auto; display: none;">
<li>
...
</li>
</ul>
</div>
Java code to locate the link is:
By xpath=By.xpath("//a[contains(#title,'Manage Users')]/img");
WebElement manageUsers = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(xpath));
manageUsers.click();
It finds the element but I get error:
org.openqa.selenium.ElementNotInteractableException: Cannot click on element
The ids are generated dynamically so we can't find by id and image source is used by multiple links.
Thank you for your help.
* Update *
The problem was solved with help from JeffC and Xwris. JeffC's last comment showed that there are multiple nodes being found. So, I added following code:
List<WebElement> manageUserImages=driver.findElements(xpath);
for (WebElement manageUserImage:manageUserImages) {
if (manageUserImage.isDisplayed()) {
manageUserImage.click();
}
}
Since there is only element displayed at one time with "Manage Users" as title, this finds the correct elements and delivers the desired results.
#JeffC, if you can post an answer with your comment, we can mark that answer as the correct answer.
Thanks again to everyone who helped.
It looks your xpath is wrong.
Personally I would start from the div and the drill down to the actual < a > tag.
In some cases where your web-element sits under a < li > tag, I would go even further up the tree and select a div which is not hidden.
i.e you instruct it to search for under the specific < div >
Who told you you can select only by id? You can use anything! :)
This should work.
//div[#class='x-tree-node-item']//a[#title='Manage Users']
This should work as well. Correct usage of 'contains' is as follows:
//div[#class='x-tree-node-item']//a[text()[contains(.,'Manage Users')]]
Hope this helps!
PS. notice that text contains is case-sensitive and will match partial text.
So if you searched for:
//a[text()[contains(.,'age User')]]
it will still be a successful match!
Update after OP's comments:
You don't actually need xpath helper. You just hit F12 in your browser and then CTRL+f so you open a search field at the bottom. Please see my example on how I locate the title of your question with partial text match ('Image').
Also notice next to xpath where it says 1 of 1 (meaning that our element is unique). Try to do the same for your case. I suspect that you need to go higher up the tree and start from an earlier < div > so you can locate the rest.
Leave off the "/img" part of your locator. You want to click the anchor (a) not the image itself.
By xpath=By.xpath("//a[contains(#title,'Manage Users')]");
WebElement manageUsers = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(xpath));
manageUsers.click();
Alternatively, the locator could be: //a[#id='m-22']

How to find elements in DYNAMIC page using selenium - 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();

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 .

Select by "name" in JSoup

I have multiple div's in a webpage URL that I have to parse which have the same class name but different names with no id's.
for eg.
<div class="answer" style="display: block;" name="yyy" oldblock="block" jQuery1317140119108="11">
and
<div class="answer" style="display: block;" name="xxx" oldblock="block" jQuery1317140119108="11">
I want to select data and parse from only one of the div's say namely (name="yyy") (the content inside the div's are <href> links which differ for each class.
I've looked up the selector syntax in the Jsoup webpage but can't get a way to work around it. Can you please help me with this or let me know if I'm missing something?
Use the [attributename=attributevalue] selector.
Elements xxxDivs = document.select("div.answer[name=xxx]");
// ...
Elements yyyDivs = document.select("div.answer[name=yyy]");
// ...

Categories