Brackets in XPath expressions in SeleniumIDE - xpath

I have a problem with SeleniumIDE's evaulation of XPath. SeleniumIDE seems to not know the round brackets in this xpath:
(//span[#class='section-name entry-box'])[last()]/input
It cannot find even this xpath:
(//span[#class='section-name entry-box'])[last()]
or
(//span[#class='section-name entry-box'])/input
But in FireFox FireBug console or in FireFox plugin XPather, all the xpaths works and finds one (or in the last example two) elements on the page.
Do you know any solution? I need to choose last element, which has specific class.

Put "xpath=" at the beginning. Selenium locators can be a variety of different types, and Selenium assumes the type is XPath if the locator starts with "//". Your's doesn't, so you need to specify it explicitly.

Related

Watir how to click on nested element

I'm trying to click on "Mr" from the drop down list I've tried a combination of things but non of them seem to work.
I've even tried xpath which is usually reliable but for this case its failing.
$browser.element(:xpath, "/html/body/div[1]/div[1]/div[1]/div/div[2]/div[1]/div[2]/div/div[2]/div/div[2]/div[2]/form/div[2]/div/div[2]/div/div/div/div/div/ul/li[2]/a").click
The XPath suggested by Saurabh Gaur, can be written in a more readable Watir-like fashion using:
$browser.ul(class: 'dropdown-menu').link(text: 'Mr').click
Note that this assumes that there is only one ul element with class dropdown-menu. If there are multiple, you will need to scope the search to the specific dropdown using an element that likely exists higher in the DOM.
However, given there is likely only one link with text "Mr", you can probably get away with simply:
$browser.link(text: 'Mr').click
Given the link is a dialog that switches from hidden to visible, you may need to also wait:
$browser.link(text: 'Mr').when_present.click
Your xPath is positional which depends on element position.. it will not work if elements are change their position means adding some elements after some action on the page.
After seeing your attached image I have generated following xPath as below :-
//ul[contains(#class, 'dropdown-menu')]/descendant::span[contains(.,'Mr')]/parent::a
Try with this xPath.. May be it will work...:)

What is the difference between Relative XPath and Minimal XPath?

Is there any difference between relative XPaths and minimal XPaths or both are same?
In Firebug there are two types of XPath mentioned in the options: 'XPath' and 'Minimal XPath'.
The difference between the two options is described within the documentation to the HTML panel.
The option Copy Minimal XPath is meant to make the XPath, which relates to one element, as short as possible. So the word 'minimal' actually refers to the length of the resulting XPath.
It is currently (Firebug 2.x) only available for elements, which have an ID. And for those elements it copies the XPath in the form of
//*[#id="elementID"]
where elementID represents the ID given within the id attribute of the element. So the words 'minimal' and 'relative' actually mean the same at the moment. Though future versions of Firebug may extend the feature to produce minimal XPaths for elements without ID. And those minimal paths don't necessarily have to be relative.
The option Copy XPath is available for all elements and copies an absolute XPath to an element, which e.g. looks like this:
/html/body/div/div[1]/div/div/table[4]/tbody/tr[17]/td[2]/a

Would like to use wildcard within an xpath

I have two links on a page that have the text 'London'. I want to choose the second one on the page, but I want to define the xpath in a way that it chooses by the parent div, but I want to use wildcard in case the link is moved.
So, the two xpaths are
//div[#id="first-id"]/div/div[2]/a[text()="London"]
//div[#id="second-id"]/div[2]/div[3]/div/a[text()="London"]
I want to use a wildcard and define the xpath within the parent div:
i.e. //div[#id="second-id"]/*/a[text="London"]
I already understand I can just use the full xpath and not have any wildcards, but I want to know if there's a way to do what I am proposing using xpath. I thought maybe contains() in some way would work but am not familiar enough with it.
To find the a element wherever it may appear within the div element, the descendant path is represented simply by //:
//div[#id="second-id"]//a[text="London"]

I have xpath written in Selenium RC & now I want to write the same xpath in selenium Webdriver. below is the xpath

I have xpath written in Selenium RC & now I want to write the same xpath in selenium Webdriver. below is the xpath:
"//table[#id='lgnLogin']/tbody/tr/td/table/tbody/tr[4]/td"
by using this xpath, i am capturing the error message displayed on my application like "Please check your Password".
Now how can I write it in Webdriver. I have different ways but, not worked out.
"String msg= driver.findElement(By.xpath("//*[#td='error2']")).toString();" - This is what i did in Webdriver.
Please help me out on this...
The XPath hadn't change from Selenium RC to WebDriver.
If you were able to use the some XPath-expression before it should work in WebDriver too (in 99% of cases as usual).
The code below should work but it's pretty hard to answer without seeing your HTML.
String msg= driver.findElement(By.xpath("//table[#id='lgnLogin']/tbody/tr/td/table/tbody/tr[4]/td")).getText();
//*[#td='error2'] - this expression looks bad as it means "any element with attribute named "td" and value "error2". I suppose that you don't have 'td' attributes in your HTML.
And to the end. I wouldn't recommend to use such long XPath expressions as in your example. It might be broken due to any minor change in layout. It's better to use some specific attributes rather than long hierarchy.
A good way to start would be to start with the basics : http://seleniumhq.org/docs/03_webdriver.html#introducing-the-selenium-webdriver-api-by-example
What findElement would do is just locate the element that you want to interact with. After you locate the element you need to perform the action that you want to do with the element. In your case, you want the text inside the element. So you can look at appropriate functions of the WebElement interface and use the appropriate method, in your case, getText.

Selenium WebDriver CSS equivalent to xpath not contains

I want to avoid using xpath in my java selenium code but cannot figure out what the css equivalent to the code below would be
("xpath", "//div[#class='error'][not(contains(#style,'display: none'))]");
Does anyone know if there is a CSS equivalent to xpath not contains
You can't easily match against a compound attribute (i.e., style=) in CSS. The not part is easy - CSS3 has a :not(...) selector. You're going to have to find something else to identify the elements you want to exclude, and then use :not(...) to exclude them,

Resources