How to get the xpath for a link? - firefox

I'm writing selenium test for a wicket application and having trouble to get some xpaths.
For example:
I'm trying to get the xpath for a link. Here is the the link created by the wicket dynamically:
<span>
<a id="id2b3" href="#" onclick="var wcall=wicketAjaxGet('./participante_geral?7-1.IBehaviorListener.0-containerDiv-frameSetDiv-gridDiv-linhas-body-rows-1-cells-2-cell-linkPanel',function() { }.bind(this),function() { }.bind(this), function() {return Wicket.$('id2b3') != null;}.bind(this));return !wcall;">
<span>Some String</span>
</a>
</span>
I'm trying to get the xpath for the link like this:
//a/span[(text() = 'Some String')]/..
But when I run my selenium test the WebDriver for firefox, I can't locate the element/link.
When I run
$x("//a/span[(text() = 'Some String')]/..")
in chromium console I can locate the element with success.
But when I run the same command in firefox I get an array containing the link.
What is wrong with my xpath?
Thanks in advance.

Try to follow the following syntax when creating a xpath
//tagname[#attribute='value']

Try to use contains function-
//a[#id='id2b3']/span[contains(text(), 'Some String')]

Wicket supports rendering an extra attribute to each component for this use case, see IDebugSettings#setOutputComponentPath(boolean) [1]. This will render wicketpath attribute. You can use it like: //a/[#wicketpath='grand:parent:child:id'].
This is for Wicket 1.5.x/6.x.
For Wicket 7.x+ there is DebugSettings#setComponentPathAttributeName(String). If it is non-null then Wicket will use this String as a name for the attribute, e.g. data-wicket-path.
https://github.com/apache/wicket/blob/wicket-1.5.x/wicket-core/src/main/java/org/apache/wicket/settings/IDebugSettings.java#L39

Related

How to click a link in Selenium using Ruby?

I have some links that are not buttons, each row of the table result has one link called View :
<a class="view-link" aria-label="View" href="/applicant_submissions/8">
<i aria-hidden="true" title="View" class="glyphicon glyphicon-folder-open icon-spacing"></i>
Can you please show me how to use Selenium using Ruby to click on this link?
Use this code:
driver.find_element(:class, "view-link").click
I don't know Ruby so, please, find my solution in Python.
You can use these locators:
Using class name:
driver.find_element_by_class_name('view-link').click
Or
Using XPath:
driver.find_element_by_xpath("//a[#href="/applicant_submissions/8']").click

Ruby Watir - how to select <a onclick="new Ajax.Request

Hi I'm trying to select an edit button and I am having difficulty selecting it.
<td>
<a onclick="new Ajax.Request('/media/remote/edit_source/3', {asynchronous:true, evalScripts:true}); return false;" href="#">
<img title="Edit" src="/media/images/edit.gif?1258500617" alt="Edit">
</a>
I have the number at the end of ('/media/remote/edit_source/3') the which changes and I have stored it in #rep_id variable.
I can't use xpath because the table changes often. Any suggestions? Any help is greatly appreciated. Below is what I have tried and fails. I am fairly new to watir and love it, but occasionally I run into things like this and get stumped.
browser.a(:text, "/media/remote/edit_source/#{#rep_id}").when_present.click
The line:
browser.a(:text, "/media/remote/edit_source/#{#rep_id}").when_present.click
fails because:
The content you are looking for is in the onclick attribute (rather than the text)
The locator is passed a string for the second parameter. This means that it is looking for something that exactly matches that. Given that you are only using part of the text/attribute, you need to use a regexp.
If you are using watir-webdriver, there is support for locating an element by its :onclick attribute. You can use a regexp to partially match the :onclick attribute.
browser.link(:onclick => /#{Regexp.escape("/media/remote/edit_source/#{#rep_id}")}/).when_present.click
If you are also using watir-classic (for IE testing), the above will not work. Instead, you can check the html of the link. Checking the html also works in watir-webdriver, but could be less robust than using :onclick.
browser.link(:html => /#{Regexp.escape("/media/remote/edit_source/#{#rep_id}")}/).when_present.click
From your example, it looks like you are using the URL from the onclick event handler as a :text locator, which I'd expect to fail unless that text does exist.
You could potentially click on the img. Examples:
browser.image(:title, "Edit").click
browser.image(:src, "/media/images/edit.gif?1258500617").click
browser.image(:src, /edit\.gif\?\d{10}/).click # regex the src
Otherwise, you might need to use the fire_event method to trigger the event handler, which looks like this:
browser.link(:id, "foo").fire_event "onclick"
These are the links to the fire_event docs for watir and watir-webdriver for reference.

Selenium Web driver xpath, span locator

Selenium Webdriver. Looking to Locate New Article from following code. Please note this is under an iframe.
<img class="rtbIcon" src="/icons/16/app/shadow/document_add.png" alt="">
<span class="rtbText">New Article</span>
I have tried to locate with xpath and many other ways. But following is what I get everytime
Code : driver.findElement(By.xpath("id('RadToolBar1'):div:div:div:ul:li[3]:a:span:span:span:span"));
Result:
The given selector id('RadToolBar1'):div:div:div:ul:li[3]:a:span:span:span:span is either invalid or does not result in a WebElement. The following error occurred:
New article has no name, id so please if some one can help find me solution.
Your xpath seems to be wrong. The best way to get the xpath for any element on a page is by installing mozilla add on - Fire Bug. You can inspect any element using this add on and also copy the correct xpath of your element present on the page.
This should be your xpath -
driver.findElement(By.xpath("//*[#class='rtbText']"));
or
driver.findElement(By.linkText("New Article"));
One of these should work. Let me know if you face any problem.

Groovy htmlunit getByXPath

I'm currently using HtmlUnit to attempt to grab an href out of a page and am having some trouble.
The XPath is:
/html/body/div[2]/div/div/table/tbody/tr/td[2]/div/div[5]/div/div[2]/span/a
On the webpage it looks like:
<a class="t" title="This Brush" href=http://domain.com/this/that">Brush Set</a>
In my code I am doing:
hrefs = page.getByXPath("//html/body/div[2]/div/div/table/tbody/tr/td[2]/div/div[5]/div/div[2]/span/a[#class='t']")
However, this is returning everything in there instead of just the url that I want.
Can someone explain what I must add to get the href? (also it doesn't end with .html)
You are selecting the a. You want to select the a/#href.
hrefs = page.getByXPath("//html/body/div[2]/div/div/table/tbody/tr/td[2]/div/div[5]/div/div[2]/span/a[#class='t']/#href")

how do I assert that a hyperlink points to a given URL?

I am using Selenium IDE and Selenium RC to check links on a website, but I am having trouble finding a way to assert that a link points to a specified url and has the specified text.
Constraints:
locating the element by dom or xpath is not feasible for it will make the test brittle
there are multiple link elements with identical link text
The HTML:
link text
link text
The test I'd like to do (rspec):
page.is_element_present?("Href=path_y, Link=link text").should be_true
#locator parameters are in order of preference
Any ideas?
i guess get_attribute should help. you should get href from link, it is attribute.
You could use isElementPresent as follows:
assertTrue(selenium.isElementPresent("//a[text()='Example Link' and #href='http://www.example.com/']");
Given the following HTML:
<html>
<body>
<a id="myLink" href="http://www.example.com">Example Link</a>
</body>
</html>
You could use the following Selenium commands (using Java/TestNG, and Selenium 1.x)
assertEquals(selenium.getText("id=myLink#href"), "Example Link");
assertEquals(selenium.getAttribute("id=myLink#href"), "http://www.example.com/");
Using Selenium 2.x the example would be:
WebElement myLink = driver.findElement(By.id("myLink"));
assertEquals(myLink.getText(), "Example Link");
assertEquals(myLink.getAttribute("href"), "http://www.example.com/");

Resources