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

1
  • is that your actual id div#h4clock a.location? If you have iframes you have to focus on the iframe prior to findElement(...). Commented May 12, 2015 at 11:54

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

6 Comments

I took slightly different approach but it seems like this one also failed.
WebElement city = driver.findElement(By.id("div#h4clock a.location")); String getcity = city.getText(); System.out.println(getcity);
A basic but very important questions. How do know the selector wasn't an ID.
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. Basically, looking at the syntax I was sure it was not id
@Saifur: can you please explain your code? I have never encountered/used "equals" like this.. So, just curious what happens here.. Thanks in advance.. :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.