How to identify button without Xpath in selenium - selenium

Code is below
<button type="submit" class="login-button">Login</button>
In selenium I tried below code:-
driver.findElement(By.classname("Login")).click();
please help me in this code without Xpath

Your classname is login-button not Login
driver.findElement(By.classname("login-button")).click();
You can use also partialLinkText
driver.findElement(By.partialLinkText("Login")).click();
partialLinkText is looking the Sub-String on HTML DOM
You can use also linkText
driver.findElement(By.linkText("Login")).click();
LinkText is looking the same String on HTML DOM
Using CSS-Selector
driver.findElement(By.cssSelector("button[class='login-button']")).click();
Hope it will help you :)

I always Prefer Cssselector rather than Xpath, it's up to the user to choose which they want and what they are comfortable with finding element.
The below link will be very useful if you want to know about CSSSELECTOR.
http://www.w3schools.com/cssref/css_selectors.asp
driver.findElement(By.cssSelector(".login-button")).click();
My suggestion would be Please inspect element and open console
$('.login-button')
Try this one until you get the required element you want. In that way you will be more flexible in getting the most required element.

Related

Unable to click on href element

I have element as below
<td>
247137
</td>
On main screen link appear as "247137" on which I need to click.
I tried as
driver.findElement(By.xpath("driver.findElement(By.xpath(".//*[#id='theCase']/tbody/tr/td[3]/a"))).click();
but it's not clicking the element and returning NoSuchElementException.
I tried various ways like till the element visible, JavascriptExecutor but no luck.
Following xpath may help you:
driver.findElement(By.xpath('//*[contains(text(),'247137')])
Hope it will help you.
Don't forget that selenium web driver supports linkText and partialLinkText so if the text is unique you may be able to use the examples below.
Here is a Java example of find by "linkText" then click:
driver.findElement(By.linkText("247137")).click();
Or you could also use "partialLinkText" then click:
driver.findElement(By.partialLinkText("247137")).click();
Thanks this resolved.
WebElement Clickusecaseid = driver.findElement(By.xpath(prop.getProperty("Clickusecaseid")));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", Clickusecaseid);

Finding XPath on Selenium

please help me.
Actually I want to find element locator in selenium using XPath since the id is auto-generate (the ID always changed when the page refresh). but the XPath always changed too. here is the XPath locator :
html/body/div[4]/div/div[1]/div/table[1]/tbody[2]/tr[1]/td/div/nobr
html/body/div[4]/div/div[1]/div/table[1]/tbody[2]/tr[1]/td/div/nobr
html/body/div[15]/div/div[1]/div/table[1]/tbody[2]/tr[1]/td/div/nobr
html/body/div[7]/div/div[1]/div/table[1]/tbody[2]/tr[1]/td/div/nobr
Actually I already try to use :
html/body/div[class='scrollingMenu']/div/div[1]/div/table[1]/tbody[2]/tr[1]/td/div/nobr
the div itself has unique classname scrollingMenu. but it is not working. it always give the error element not found.
You can use element's id in your XPath as follow:
Suppose id="constantPart-generatedPart12345", then XPath is
//*[contains(#id, "constantPart-")]
PS. If this not works, update your question with HTML for target element, so I can edit XPath appropriatelly
Open the page in Google Chrome and use F12 to test your xPath before implementing it - has saved me a lot of time

Selenium Webdriver Onlink Text

What command in webdriver should I use to make sure it clicks the specific text i.e Last Month
the code is
<div class="dt_padded_medium_div">
<a onclick="setLastMonth()" href="#">Last Month</a>
I tried xpath by using firepath but still doesnt work
it was
//*[#id='block-2']/div/div[3]/table/tbody/tr[1]/td[5]
I used
driver.findElement(By.xpath("//*[#id='block-2']/div/div[3]/table/tbody/tr[1]/td[5]")).click();
but still didnt work, am I missing something?
Update:
Got the Code working guys, thanks for the help!
If there is only one 'a' element in the page with this text, try this XPath:
//a[text()='Last Month']
If there are more than one element, please, post the full HTML tree else we're unable to write a xpath without know the tag path and ensure it will work
Are you sure your xPath is correct?
Install FirePath on Firefox and experiment with your xPath as on screenshot below.
Also, that does look like a long and brittle xPath. Look up xPath cheat sheets to learn how to make your xPath more robust.
In your case I would imagine something like:
//*[#id='block-2']/descendant::a[text()='Last Month']
(get the element with a particular ID, then search for an <a></a> in that element, no matter how deep, with a particular text)
In your question, you have used a "id" that is not shown in your code, where is #id='block-2'. It is possible for you to have used the wrong id. For us to help you, can you please provide the whole HTML code?
What I can suggest based on the information you have provided is:
Making sure the Xpath you have provided is unique, there is an add-on for Firefox called Firebug you can use. It will help you find out the xpath you are after fast and easy. What you need to do is basically:
download firefox and install it;
download firebug add-on and install it to firefox;
you will notice there is a small bug symbol to the top right of your tool bar, please enable it;
you will see a console pops out, click on inspection button and click on any web element you want to inspect and its html code will be highlighted in the console;
right click on the highlighted code and choose to copy its xpath to clipboard. This way you will never get your xpath wrong.
Here is a quick tutorial:
http://www.wikihow.com/Find-XPath-Using-Firebug
P.S. There are more than one way to locate a webelement, please consider using the following options as well:
Css selector
class name
id
This link will direct you to an awesome cheat cheet.
http://scraping.pro/res/xpath-cheat/xpath_css_dom_recipes.pdf
Hope it helps.
Please try to use below code to click on the text Last Month
driver.findElement(By.xpath(".//a[text()='Last Month']")).click();
Hope this helps.
You can use below code to click on the link containing the text 'Last Month'
driver.findElement(By.linkText("Last Month")).click();
First of all make sure that your xpath works correctly, try to use more specific xpath other than just td[5] or smthn like that, for example above could be like:
//div[#class='dt_padded_medium_div']/a[#href] - meaning, we need to find <div> element with specific parent and within it <a> element that has href property
There are a few possibilities.
If you're site doesn´t get rewritten to much you could just go for the link name via
driver.findElement(By.linkName("Last Month");
You could also go for any of the attributes if your site is subject of many rewrites like this and just put "href" and "#" or "onclick" and "setLastMonth()" as arguments when calling it.
static WebElement getLink(String Attribute, String Value /*String ItemText*/){
List<WebElement> Elements = driver.findElements(By.tagName("a"));
for(int Counter = 0, Counter < Elements.size(); Counter++){
if(Elements.get(Counter).getAttribute(Attribute).contains(Value) /* && Elements.get(Counter).getText().equals(ItemText){
return Elements.get(Counter);
}
}
return null;
}
If you remove the comments it will go completely sure and check for the Attributes and the displayed text.

Not able to locate element in <a> tag having href as javascript

This is the html code
I have tried these-
.//*[#id='contact_list-menu-contact_add']
//a[contains(#name,'add')]
But no success.
Switch to frame and click on the link:
driver.switchTo().defaultContent();
driver.switchTo().frame("FRAME NAME");
driver.findElement(By.xpath("//a[#id='contact_list-menu-contact_add']")).click();
You can try to use a cssSelector.
I use Selenide a good wrapper for Selenium. I would then search for your html example like this:
$(By.cssSelector("a[id='contact_list-menu-contact_add'][class*='add']"))
If you want to find <a> tag mentioned in your comment,you do not need to write //a[contains(#name,'add')] after your xpath mentioned above.
The below mentioned xpath will work
//*[#id='contact_list-menu-contact_add']
Let me know if you are looking for something else.

Selenium object identification

I am using Selenium webdriver to test my application & i am facing difficulties in identifiying button on the same. the code snippet is like :
<input type="submit" onclick="return sign(this);" value="Login">
and its xpath is :
html/body/table/tbody/tr[2]/td/center/form/center/table/tbody/tr[3]/td/center/input[1]
Which object property to use and how?
You should not use that XPath.
I would hazard a guess that you used some sort of tool, whether it's Firebug or IDE, to generate that XPath. Stop that now!
XPath is fine to use, and can be used here, just not relying on the tools to generate it for you! That XPath is destined for failure!
You will need to provide more HTML, specifically around that button.
However, you should just be able to use something as simple as:
//input[#value='Login']
You can use the xpath, if that is really stable. I found that it is much easier to define id tags in the html elements and the use a By.id locator. Alternatively you can use css selectors, depending on the "uniqueness" of your button something like this could work:
By.cssSelector("input[value='Login']")