Selenium web driver can't find element - java

I have a problem when trying to find a Web Element using Selenium web driver.
I've done this with two pages, which both of the elements I'm wanting to grab are in lots of classes, ids, etc.
Here's the first one element, which I had no problems finding.
<input type="text" class="input-large" name="host" value="">
Which I used this to grab:
WebElement HtmlHost = driver.findElement(By.name("host"));
Now here's where my problem begins. I'm now trying to grab this:
<button type="submit" name="bootBtn" class="btn btn-inverse btn-large">Start hashing</button>
Which I've tried grabbing with all of these functions, but none have been able to find the element.
driver.findElement(By.partialLinkText("Start "));
driver.findElement(By.linkText("Start Hashing"));
driver.findElement(By.name("bootBtn"));
driver.findElement(By.cssSelector("button[type='submit']"));
What do you suggest I do?
Thanks.

driver.findElement(By.xpath("//button[contains(text(),'Start hashing')]"));
This will grab the element.

Okay I found the problem, and I feel pretty stupid that I missed this. The site I was trying to find the button requires you to login. I already knew this as I've coded the login process, but when I created this new void I placed it before the login process.
Sorry to waste time, thanks for help!

Try this
Thread.sleep(2000);
driver.findElement(By.xpath("//button[#type='submit']"));

Related

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

Selenium with Java and Eclipse and SmartGWT

first time here and I tried to search for this info, so please forgive if this has been asked before. I am fairly knew to coding/selenium. I don't seem to have an issue with anything other than our SmartGWT screens and wondering if anybody could help with suggestions...I can't seem to get Selenium to work at all withing anything SmartGWT related actually
For example I have a date field and I want to type in a new date
This is the code according to firepath
<input id="isc_B" class="selectItemText" type="TEXT" tabindex="1131"
style="width:66px;height:12px;-moz-user-focus:normal;"
autocomplete="OFF" onselect="if (window.isc_ComboBoxItem_0 == null)
return;isc_ComboBoxItem_0.$54h()"
oninput="isc_ComboBoxItem_0._handleInput()" spellcheck="true"
$377="$378" $376="isc_ComboBoxItem_0" handlenativeevents="false"
name="valueField"/>
According to firepath these are the xpaths for it
[.//*[#id='isc_B']
html/body/div[4]/div[1]/div[2]/div/form/table/tbody[2]/tr[2]/td[1]/input
and I've tried these:
driver.findElement(By.xpath(".//*[#id='isc_B']")).sendKeys...;
driver.findElement(By.xpath("//*[#id='isc_B']")).sendKeys...;
driver.findElement(By.id("isc_B")).sendKeys...;
and can't get anything to recognize. any pointers would be awesome and thank you in advance. Do I need to import specific libraries for smartGWT or something?
Don't go by the XPaths suggested by FirePath..Try to create relative XPaths..Because absolute XPATHs can change..
For the above input tag I would recommend you use something like below
driver.findElement(By.className("selectItemText")).sendKeys("yourDate");
EDIT: Based on your website below is the code which works absolutely fine for me:
WebDriver driver=WebDriverFactory.getBrowser("http://www.tobymob.com/test/test.html");
driver.findElement(By.className("selectItemText")).sendKeys("someDate");

Xpath does not work with Selenium

I'm trying to build tests for an old system. The HTML is not well formed. I need to identify and click a radio button.
The html looks like this:
...
<td class="tablerow" colspan="3">
<INPUT type=radio name="ticket" value="22" >ramdom1
<INPUT type=radio name="ticket" value="1" >ramdom2
<INPUT type=radio name="ticket" value="3" >ramdom3
<INPUT type=radio name="ticket" value="99" >ramdom4
</td>
...
I was trying to select the input using xpath as follows:
String xpath = "//input[contains(#name, 'ticket') and contains(#value, '3')]";
WebElement rb = driver.findElement(By.xpath(xpath));
But selenium doesn't found the element.
If change it to
String xpath = "//input[contains(#name, 'ticket')]";
List<WebElement> rbs = driver.findElements(By.xpath(xpath));
or
String xpath = "//input[contains(#value, '3')]";
List<WebElement> rbs = driver.findElements(By.xpath(xpath));
It works, selenium returns a list of elements, including the one that I need. The problem occurs only when I try to use both conditions in a same xpath.
Of course that I could iterate over the list and test each value, but I would like to understand if I'm doing something wrong or not. Since IE doesn´t have native xpath support, I thought this could be a selenium implementation issue.
I'm using Selenium WebDriver (2.37.1) with IE Driver.
Not sure whether this is a Selenium implementation issue but this should work:
"//input[contains(#name, 'ticket')][contains(#value, '3')]"
The use of and is basically the same so the result should be correct here.
I am unsure why that doesn't work, and this technically isn't an answer, but you can replicate precisely what Selenium does to ensure it's not Selenium or any of it's tools at fault.
Selenium uses a library called "Wicked Good XPath" for a Javascript-based implementation of an XPath engine because IE doesn't have a "native" one.
So, to reproduce the scenario, take a copy of your page and add Wicked Good XPath to the script headers. Documentation on the front page of that website is very simple, and very easy to follow.
Once loaded in IE, open the Developer Tools and go into the Console. Wicked Good XPath will need to be "initialised" as such, and therefore you'll need to call wgxpath.install() in the console.
Once done, you now have access to the same library that Selenium would be using. Now, you can call a function within IE's developer console to access the DOM using XPath:
document.evaluate("//input[contains(#name, 'ticket') and contains(#value, '3')]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue
The exact element you need will be returned, at least for me.
Now, admittedly, you don't need XPath at all for this, you can get away with using CSS selectors:
input[name~='ticket'][value='3']
We can use the following css
1.css=input[value='22']
2.css=input[value='1']
3.css=input[value='3']
4.css=input[value='99']
For Checking the Radio Buttons.

sending request to a page

I'm trying to fill-out a form automatically and press a button on that form and wait for a response. How do I go about doing this?
To be more particular, I have a a --HUGE-- collection DNA strains which I need to compare to each-other. Luckily, there's a website that does exactly what I need.
Basically, I type-in 2 different sequences of DNA and click the "Align Sequences" button and get a result (the calculation of the score is not relevant).
Is there a way to make a Java program that will automatically insert the input, "click" the button and read the response from this website?
Thanks!
You can use the apache http client to send a request to a web site.
Look at the source to the page in question, and you'll find the part. This contains all the fields that need to be sent to the server. In particular, you'll see that it needs to be sent as a Post, rather than the more common Get. The link above shows you how to do a post with the http client code.
You'll need to provide a nameValuePair for every field in the form, such as these ones:
<input type="hidden" name="rm" value="lalign_x"/>
<input type="checkbox" name="show_ident" value="1" />
<textarea name="query" rows="6" cols="60">
It will probably take some trial and error for you to get all the fields set up correctly. I'd recommend doing this with small data sets. Once it all seems to be working, then try it with your bigger data.
In Python you can use mechanize library (http://wwwsearch.sourceforge.net/mechanize/). It's quite simple and you doesn't need to know Python very well to use it.
Simple example (filling login form):
br = Browser()
br.open(login_link)
br.select_form(name="login")
br["email"] = "email#server.com"
br["pass"] = "password"
br.submit()
You could probably do this using Selenium.

Enter Key Press behave like Submit in JSF

How to make Enter Key Press behave like Submit in JSF. It works with InputBoxes; but not with inputSecret boxes
I haven't seen this issue before. The chance is little that this behaviour is browser specific. Try in different kinds of browsers to exclude the one and other (IE6/7/8, FF, Safari, Chrome, etc). The chance is bigger that this is caused by a (poor) Javascript key event listener which incorrectly suppresses the enter key (for example a JS password validator which checks the entered characters).
If still in vain, just add the following onkeypress to the h:form:
<h:form onkeypress="if (event.keyCode == 13) this.submit();">
You need to take textareas into account however. If you have them, then you need to copy all onkeypress events over the h:inputXXX elements expect of textareas yourself.
If you want to press ENTER key instead of submit in any form, what we have to do here is add defaultcommand attribute in af:form tag and give id of the submit button as value. Sample code snippet for this is
<af:form id="f1" defaultCommand="cb1">
<af:outputText value="User Name" id="usename"/>
<af:inputText value="#{BackingBean.userName}" id="uname" />
<af:outputText value="Password" id="pword"/>
<af:inputText value="#{BackingBean.password}" id="paword" secret="true"/>
<af:commandButton text="Submit" action="#{BackingBean.method}" id="cb1" />
</af:form>
I made it work by placing an additional inputBox and hiding it using javascript. Let me know if you have any other suggestions
Thanks baluC for pointing me in the right direction
Jerry
tested with jsf 2. put:
<h:commandButton id="hidden" style="visibility: hidden;" action="#{mybean.myaction()}" />
in your form
From ComputerPilot's answer I came to know that it's bug in IE which wouldn't make the parent form to submit if there's only one input element. To overcome this problem I added one more input Box with attribute style="display:none" and it worked fine.
There is an old specification that pops into my mind with this one. If you have a form that contains just one input field, the behaviour of submitting on the enter-key doesn't work in some versions of Internet Explorer. The easiest fix is to make sure you have more than one input field.
Other reasons for this problem include...
Not having an input of type submit
Having an input of type submit, but it isn't visible on the page (hidden or positioned off-page)
This behaviour is very specific to this browser.
MS Bug Report Here: connect.microsoft.com: submit button value not posted with form submission

Categories