How to find a Web element using a href attribute

advertisements

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 = "http://www.bing.com/images?FORM=Z9LH";
driver.findElement(By.cssSelector("a[href*='" + href + "']")).click()

If you inspect www.bing.com 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:

driver.findElement(By.cssSelector("li#scpt0>a")).click();