29

This question received great answers in jquery and I was wondering if someone could give an example of this in Java please?

I'm doing driver.findElement(By.className("current time")).click(); The space is the issue, and I see the explanation at the link, but I'm not sure how to handle it in java, and don't have access to change the class name.

Pasting example of what i get in the firefox inspect id: Example with cssSelector below did not work, but i may be missing something.

<span>
<a class="current time" href="http://someurl/"   onclick="s_objectID="http://someur/">url</a>
</span>
Community
  • 1
  • 1
Green
  • 1,405
  • 4
  • 21
  • 27

3 Answers3

60

Instead of class name you can use a css selector. You don't mention the tagname for the class 'current time'. I am assuming it to be input, so your css selector work be,

WebElement element = driver.findElement(By.cssSelector("input[class='current time']"));
element.click();

Edit#1 Based on html provided,

Looking at the html in your comment, it seems you have quite a few options to find the webElement. Here are your options,

WebElement element = driver.findElement(By.cssSelector("a[class='current time']"));
element.click();

or this should work too,

WebElement element = driver.findElement(By.cssSelector("a.current.time"));
element.click();

You can also use linkText since the element is link. From the html you provided, the link text is 'url'

WebElement element = driver.findElement(By.linkText("url"));
element.click();

You can also use By.partialLinkText("partial link text here");

You can also use xpath as:

WebElement element = driver.findElement(By.xpath("//a[@class='current time']"));
element.click();

OR,

WebElement element = driver.findElement(By.xpath("//a[text() = 'url']"));
element.click();
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
nilesh
  • 14,131
  • 7
  • 65
  • 79
  • Hi, thanks for helping. That did not work for me, I'll paste the piece that i'm trying to click. This works when the classname is one word, but it's two words in some like the example below. I tried the cssSelector but i'm new to this and could be easily missing something. url – Green Sep 20 '11 at 13:16
  • 1
    I updated the answer based on the html you provided for better viewing. Try it out and let us know – nilesh Sep 20 '11 at 14:09
  • Thanks for the help. I had to use linktext which I was trying to avoid, I'm new to this so could not currently say why the other examples did not work. – Green Sep 21 '11 at 13:14
  • Thanks, for my case is button and the following is works for me after long struggle WebElement element = driver.findElement(By.cssSelector("button[class='current time']")); – Singaravelan Nov 12 '15 at 07:09
5

For a less fragile test, another option is to use an XPATH which doesn't depend of the order of classes, like:

WebElement element = driver.findElement(By.xpath("//a[contains(@class, 'current') and contains(@class, 'time')]"));
user859740
  • 151
  • 2
  • 2
5

Whenever you found some space in the class name you need to switch to cssSelector Locator. Convert a class name to cssSelector if it is having a space as below.

In your case it would be:

WebElement element = driver.findElement(By.cssSelector(".current.time"));
element.click();

PS: add . [dot] in start of class name and replace the space with . [dot] to convert class name to cssSelector.

invzbl3
  • 5,872
  • 9
  • 36
  • 76
Gaurav
  • 197
  • 1
  • 7