How to find a Web element using a href attribute


It's pretty straight forward. I have a String which contains an href value. I want to find the web element on the page that contains that specific href. I am using:

String href = "";
driver.findElement(By.cssSelector("a[href*='" + href + "']")).click()

If you inspect you can see that the first link "images" on the top left has the href value that I set here. When I run, it says that there is no such element on the page. I feel like I am not using the correct "find by" option.

This should work for you: