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

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.

Related

Locate a button which has a span with specific text in Selenium python

I have following html snippet:
<button><span>Next</span></button>
I want to locate this button and I am using the following:
driver.find_element_by_xpath("//button[#span[.='Next']]")
Also used:
driver.find_element_by_xpath("//button//span[.='Next']")
but this locates the span instead of the button :(
But no luck with this. Does anyone know what is the correct way for the same?
Try this XPATH to locate button based on child span text content
"//button[span='Next']"
Try this xpath:
driver.find_element_by_xpath("//span[text()='Next']//parent::button")

Finding link using text in CSS Selector is not working

I am trying to locate below link by using a[text='This is a link'] and a[innertext='This is a link'] but both of them are not working.
I know this can be achieved by using XPath or other ways, but I am curious why CSS Selector that I have used is not working. refer this link.
<a title="seleniumframework" href="http://www.seleniumframework.com" target="_blank">This is a link</a>
You are trying to locate a link by using the following CssSelectors:
a[text='This is a link']
a[innertext='This is a link']
As per Issue#987 and Issue#1547:
The :contains pseudo-class isn't in the CSS Spec and is not supported by either Firefox or Chrome (even outside WebDriver).
You can find a detailed discussion in selenium.common.exceptions.InvalidSelectorException with “span:contains('string')”
Solution
As per the HTML you can use either of the following solutions:
CssSelector using the attribute title:
"a[title='seleniumframework']"
CssSelector using the attribute href:
"a[href='http://www.seleniumframework.com']"
CssSelector using the attributes title and href:
"a[title='seleniumframework'][href='http://www.seleniumframework.com']"
The right CSS Selector for your case is:
a[title="seleniumframework"]
or
a[href="http://www.seleniumframework.com"]
You can also use this one: a[target="_blank"], but the ones above are more unique.
Hope it helps you!
Since you are trying to use attribute selector, you can only select from available attributes that the hyperlink element currently has.
text is not available attribute in html so selecting it this way through css will not work.Rather you should try to use other attribute like a[title=seleniumframework] for example.

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.

How to identify button without Xpath in 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.

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

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