6

I have the following HTML code:

<a href="/search/?p=2&q=move&mt=1"> 2 </a>

I would like to get what is contained in href, ie, I was looking for a command which would give me "/search/?p=2&q=move&mt=1" value for href.

Could someone please help me with the respective command and css locator in selenium, for the above query?

if I have something like:

<a href="/search/?p=2&q=move&mt=1"> 2 </a> 
<a href="/search/?p=2&q=move&mt=2"> 3 </a> 

Out of these two if I was to get the attribute value for href whose text conatins '2', then how would my css locator synatx look like?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Sunny
  • 129
  • 1
  • 2
  • 10

2 Answers2

6

If your HTML consists solely of that one <a> tag, then this should do it:

String href = selenium.getAttribute("css=a@href");

You use the DefaultSelenium#getAttribute() method and pass in a CSS locator, an @ symbol, and the name of the attribute you want to fetch. In this case, you select the a and get its @href.

In response to your comment/edit:

  1. The part after @ tells Selenium that that part is the name of the attribute.

  2. You should place :contains('2') before @href because it's part of the locator, not the attribute. So, like this:

    selenium.getAttribute("css=a:contains('2')@href");
    
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • thanks BoltClock, this was great help. I just have a couple of questions relatd to this: 1. What does @ mean? 2. if I have something like: 2 3 Out of these two if I was to get the attribute value for href whose text conatins '2', then how would my css locator synatx look like? I tried selenium.getattribute("css=div#paginationContainer a@href:contains('2')") but this gives the following error: Could not find element attribute: css=div#paginationContainer a@href:contains('2') thanks in advanvce – Sunny Aug 26 '11 at 07:48
  • Thank you so much for that, it was very helpful, it worked. I have one more query in the same regard actually. Is there a way to check whether the attribute href is present in a particular HTML tag using css and selenium. So, for example, in the above case, if I want to check if the HTML tag which conatins text '2', contains href attribute or not? Is that possible and how? Thnaks a lot. – Sunny Aug 26 '11 at 08:24
  • Try `selenium.isElementPresent("css=a[href]:contains('2')")` – BoltClock Aug 26 '11 at 08:33
  • excellent, it all worked great. One last question. This will hopefully be the last. Learnt a lot today. OK, so i need to check if some text on the page is bold or not. How would i do that? – Sunny Aug 26 '11 at 08:42
  • Not sure about that, sorry. (I know it's your last question.) – BoltClock Aug 26 '11 at 08:43
  • excellent, it all worked great. One last question. This will hopefully be the last. Learnt a lot today. OK, so i need to check if some text on the page is bold or not. How would i do that? I thought of the follwoing things but could not find a way to implemement these: 1. usually
    1.....
    is in bold, so is there a way to check if inside my 'div', if the text contains the tag or not. But i dont know how to do this in selenium? 2. Sometimes font-weight:bold is used but this is not a part of the tag itself, so i am not sure if this would work? could you please help. thanks
    – Sunny Aug 26 '11 at 08:49
2

Changing css=a@href to href should do the trick. Let me if this did not work.

    List<WebElement> ele = driver.findElements(By.className("c"));
        for(WebElement e : ele)
        {
           String doctorname = e.getText();
           String linkValue = e.getAttribute("href");
        }
Vinay
  • 648
  • 4
  • 12