Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i select this span element?

I am just starting with Selenium and now in need to select this element:

<span class=" close">Matrices</span>

This line of code returns zero elements, so i guess it's not the right one :-)

ReadOnlyCollection<IWebElement> matrixLink = driver.FindElements(By.PartialLinkText("Matrices"));

But I could not find another one suitable, besides the Xpath, but that looks like this (//*[@id=\"Navigation\"]/div[2]/div[2]/ul/li[7]/span), and that seems a bit fragile to me?

EDIT: the span has the class 'close'. It's part of a menu, where there are 19 span's with the class 'close' so it's not a unique selector unfortunately....

like image 361
Michel Avatar asked Feb 08 '13 08:02

Michel


People also ask

How do I select span text?

For a given HTML document and the task is to get the text of a <span> tag using JQuery. you can Use jQuery text() method():This method is used to set or return the text content of specified elements.

What is an element of a span?

The <span> HTML element is a generic inline container for phrasing content, which does not inherently represent anything. It can be used to group elements for styling purposes (using the class or id attributes), or because they share attribute values, such as lang .

How does selenium Webdriver select span value?

We can select the text of a span on click with Selenium webdriver. To identify the element with span tag, we have to first identify it with any of the locators like xpath, css, class name or tagname. After identification of the element, we can perform the click operation on it with the help of the click method.


2 Answers

This will work:

//*[@id=\"Navigation\"]/descendant::span[text()='Matrices']

Note that if you can, be specific in your XPath queries, mainly to aid readability and improve performance...that is the * in your query will query all elements in the page. I don't know what kind of element the Navigation element is, but you should put it's exact element type in, for instance if it's a div, make it:

//div[@id=\"Navigation\"]/descendant::span[text()='Matrices']

A slight explanation for this XPath is that it will grab the Navigation element, and simply look anywhere inside it to find a span element that has the text of Matrices. Without the descendant bit in the XPath, it would only search for direct children. That means elements that a child of Navigation, nothing else - so if an element is a child of TestDiv which is a child of Navigation, descendant would catch it, without it you won't return any results.

As for why By.PartialLinkText would not work, this would only search for anchor links. It is common, as you have seen, that anchor links have a span element inside them or sometimes it is just a span on it's own.

By.PartialLinkText and similarly By.LinkText would not 'see' this element, since it's not an anchor element.

like image 184
Arran Avatar answered Dec 13 '22 20:12

Arran


My favorite problem solver for these cases:

  • Install Selenium IDE
  • Click the link you need
  • In the "target" in Selenium IDE you will see different xpath possibilities

But I would use the approach, that its N-th element with "close" class (//span[7] or something like that)

like image 38
Pavel Janicek Avatar answered Dec 13 '22 21:12

Pavel Janicek