Is there a way to get the HTML of an element? - selenium

I'm using the FluentAutomation version of Selenium, and I can't find any way to get the HTML of an element. I can get the text and attributes, but there doesn't seem to be any way to get the full HTML content, which I need for a certain kind of test I'm doing.
Is there any way to do this?

Assuming you mean the innerHTML or the outerHTML, you should just be able to call GetAttribute on the element, or whatever it is in your language.
Example C#:
element.GetAttribute("innerHTML");
Example Ruby:
element.attribute('innerHTML')

I am not sure if a straight-forward way exists, anyway, it should be possible to execute something similar to:
((JavascriptExecutor)driver).executeScript("return arguments[0].innerHTML;", element);

Related

How can I access a button to click it with following code in selenium webdriver?

There are many buttons sharing the same class.
May have to go with clicking the link /account/logout'.
This is the code I'm trying to use:
input class="btnRed text-uppercase fo_white" value="Logout" onclick="window.location.href='/account/logout';" type="button"
Hard to say because you didn't provide enough info, but you could probably make it work by using value attribute. Something like this if you are using java.
driver.findElement(By.cssSelector("[value='Logout']")).click();
Not pretty solution, but give it a try:
driver.findElement(By.cssSelector(".btnRed.text-uppercase.fo_white")).click();
Looking at your HTML DOM this command will work for you:
driver.findElement(By.xpath("//input[#value='Logout']")).click();
Let me know if it works for you.
Looking at your HTML, this should work
driver.findElement(By.xpath("//input[#value='Logout'][#type='button']")).click();
Let me know if it works.
Is your element visible/enabled? In order to select an element, it must be present in your DOM. If the element is activated through an event, it cannot be selected until the event occurs.
Take a look at this post. This other post also have good ideas.

Access Selenium IDE output in Selenium WebDriver

I'm having this requirement in which i need to access an element on my page and want to get all the properties of the element. I have already written a webdriver script to get the id,name,css,linktext but i'm not getting the idea how to get the xpath and css selector for that element.
One thing which i'm having in my mind is select the element using Selenium Ide and in the Select tab it will get all the attribute value which is very useful for me, but how to get this result in my selenium webdriver.
I dont know whether this is possible or not but if someone can give me some reference that will be very useful.
Hope for some positive answers.
You can generate absolute xpath. Please take a look at this: https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/5520
Css and xpath selectors are based on the element attributes and/or tags.
You can write a method to generate some selectors but this does not guarantee that your selector would be efficient and it can break even when a slight change in the page is done.
Even if you are using a method like #arun-prakash suggested for xpath is the same thing. I don't see the reason behind this.
You will have to use a selector to identify the element so why get the selector again? You should ask the reason and how these selectors would be used.

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.

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']")

Selenium xpath flow

Can anyone tell me what is the exact flow of taking out xpath in Selenium-IDE.
After trying alot by putting alerts i m not getting how to take out the exact xpath.
Selenium displays the xpath according to xpath:position and some others ways also but i want to add the xpath traversing from html i.e the topmost position.How can i do that???
I've come to prefer CSS selectors over Xpath. The IDE won't create them for you, but with a little practice they're very easy to write. As a bonus, they make your tests much easier to maintain.
For example, let's say you have a button with the text "save". The locator would be:
css=button:contains('save')
Check out http://www.w3.org/TR/css3-selectors/ for more detail.
I use XPather to find out the xpath. Then I use the resulting path in the Selenium IDE to cross verify. You could also use other tools like XPath Checker or Firebug to do the same.
Write 1 line of code in javascript:
LocatorBuilders.order = ['id', 'link', 'name', 'dom:name', 'xpath:link', 'xpath:img','xpath:attributes', 'xpath:href', 'dom:index', 'xpath:position'];
change it the way you want (not all of them has to be there)
see:
chrome://selenium-ide/content/locatorBuilders.js
save as your_file.js
add your_file.js as "Selenium Core extensions" in the IDE options
Firefinder for Firebug helps matching both CSS and XPath locators
In general, with Firebug installed, you can Inspect Elements for their HTML and DOM structures to determine what best locator you can build.
the best way to find x PAth is :
open developer tool bar in chrome:
Find the x Path displayed below :
You can directly copy and paste this x path in your script