1

Please can someone point me to the right direction with below code?

driver.findElement(By.id("div#h4clock a.location").equals("London"));

I used getText("London") but it did not work.

I am quite new so any advise would be very much appreciated.

I also want to have a string to store the element London and display it using Println.

Many thanks in advance,

Hamid

Saifur
  • 15,084
  • 6
  • 43
  • 68
HK009
  • 71
  • 1
  • 5
  • is that your actual id `div#h4clock a.location`? If you have iframes you have to focus on the iframe prior to `findElement(...)`. – Fran Montero May 12 '15 at 11:54

1 Answers1

1

The selector does not look like as an id that cssSelector. Try

driver.findElement(By.cssSelector("div#h4clock a.location")).getText().equals("London");

Edit:

WebElement city = driver.findElement(By.cssSelector("div#h4clock a.location")); 
String getcity = city.getText(); 
System.out.println(getcity);
Saifur
  • 15,084
  • 6
  • 43
  • 68
  • I took slightly different approach but it seems like this one also failed. – HK009 May 12 '15 at 11:58
  • WebElement city = driver.findElement(By.id("div#h4clock a.location")); String getcity = city.getText(); System.out.println(getcity); – HK009 May 12 '15 at 11:58
  • A basic but very important questions. How do know the selector wasn't an ID. – HK009 May 12 '15 at 12:08
  • There are multiple things you can see. With `#` in css means `id` and `.` means class. And, there are some basic requirements of how an id should look like in the `html`. See [this](http://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html). Basically, looking at the syntax I was sure it was not id – Saifur May 12 '15 at 12:11
  • @Saifur: can you please explain your code? I have never encountered/used "equals" like this.. So, just curious what happens here.. Thanks in advance.. :) – Subh May 14 '15 at 08:09
  • @Subh Welcome back. Hope you have been good. Yes that code was little off. We discuss that in comments and resolved forgot to update my post. Thanks for pointing out. – Saifur May 14 '15 at 12:11