Having trouble finding a hyperlink with CSS selectors

I am attempting to utilize Selenium to retrieve some data from Google, but the CssSelector I have implemented is consistently returning "No element found".

Below is my code snippet:

//Open google page
IWebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
driver.Navigate().GoToUrl("https://www.google.com/search?q=cheese");
wait.Until(driver1 => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));

//Get image link
IWebElement image_link = driver.FindElement(By.CssSelector("a[class='q qs']"));

The error occurs on the last line and the anchor tag I'm trying to access has the following structure on the webpage.

<a class="q qs" href="/search?q=cheese&amp;client=firefox-a&amp;hs=YWQ&amp;rls=org.mozilla:en-US:official&amp;source=lnms&amp;tbm=isch&amp;sa=X&amp;ei=ewL9U-S5FNGpyATTl4CgCA&amp;ved=0CAgQ_AUoAQ">Images</a>

What could be causing this issue?

Answer №1

Stop passing the css selector a css class. Instead, you should pass it an element selector filtered by attribute. Give this a try:

By.CssSelector(".q.qs")

Answer №2

Take a look at the link above for more information. Give this code a try.

IWebElement imageLink = driver.FindElement(By.CssSelector("a.q qs"));

Also, here is an example using xpath.

IWebElement imageLink = driver.FindElement(By.Xpath("//a[@class='q qs']"));

Answer №3

In my opinion, the most reliable and explicit way to get the link by text is as follows:

IWebElement image_link = driver.FindElement(By.LinkText("Images"));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Display everything when an option is clicked

I have a filter that is working perfectly. When I select a specific category, it filters out only rows with that category. However, I am stuck on how to display all rows again after clicking on the first option. My objective is to show all rows when "Categ ...

What is the best way to close an ajax page within the main page using a button?

I am dealing with a situation where I have 2 pages. The first page contains a div called 'ajaxicall', which loads another page inside it. The challenge I am facing is figuring out how to close the second page when I click on the "close" button w ...

Troubleshooting issue with jquery.backgroundSize.js on Internet Explorer 8

After trying out the jquery.backgroundSize.js plugin, I ran into issues getting it to work in IE8. I followed the steps from the official website and tested on various browsers like IE9 and Firefox, but nothing seemed to happen on IE8. css: #main{ b ...

Ensure that the central section of the slider remains illuminated

Looking for help with customizing my "product" slider, lightSlider. I want the middle div to always be highlighted when navigating through the slider. Here's what it looks like currently: Link to screenshot - current. My goal is to achieve this look: ...

Hover Effect for Instagram Feed (Web Application)

Recently, I created an app using the Instagram API to bring Instagram images into a webapp. As part of this project, I have been diving into CSS to implement a rollover effect where a heart icon appears when users hover over an image. This heart will event ...

Issue with Selenium script implementation

I am facing a challenge as a writer. I have created a task as an admin and assigned it to myself, but now I am having trouble locating the specific task in the list of 10 tasks displayed on the page. The task could be on the first or second page, and I nee ...

refusing to display the pop-up in a separate window

Hi there, I'm having an issue with a link that's supposed to open in a pop-up but is instead opening in a new tab. I'd like it to open in a proper pop-up window. <button class="button button1" onclick=" window.open('te ...

What causes the CSS height attribute to mess up UL lists?

I'm struggling to understand a problem in my nested list. When I set the height of LI elements, they end up overlapping each other. Can someone explain why this is happening and suggest a proper way to apply height without causing this overlap effect? ...

Navigating between child nodes in an HTML drop down list with multiple options and hierarchical structure

Hey there! I am looking to create a unique HTML hierarchy drop-down menu. It will have multiple levels or options, with each option having the potential for child nodes. When an option has child nodes, an arrow will appear next to it. Clicking on this ar ...

Arranging buttons that are generated dynamically in a vertical alignment

Currently, I am in the process of creating a webpage for various items, and everything is going well so far. However, I have encountered an issue while attempting to vertically align the buttons that I am generating using JavaScript within the document. I ...

Can Bootswatch be installed using a package manager?

I have recently launched a small asp.net core website. Right now, all JS/CSS imports are from NPM and I am using webpack to bundle them together. Instead of sticking with the standard bootstrap template, I wanted to switch to bootswatch paper, but unfortu ...

Automating Angular JS pages with IE10 using Selenium

Currently, I am working on a project that involves automating pages developed in Angular JS using Selenium. The tests are running smoothly in Firefox 25 on a Windows machine. However, when I attempt to run the same tests through IE 10, I encounter random f ...

Translating a C# ViewModel into TypeScript

Here is a C# viewmodel example: public class LoginModel { [Required(ErrorMessage = "Email is not specified")] public string Email { get; set; } [Required(ErrorMessage = "No password is specified")] [DataType(DataType.Password)] public ...

How can I design a form that resembles the sign-in form used by Google?

Currently, I am in the process of creating a contact form for a website that is inspired by the design of Google's material sign-in form. I have successfully implemented an effect where clicking on the input field causes the label to change its posit ...

I am perplexed by JQuery. It is returning true when comparing an input value from a number type input, and I cannot figure out the reason behind it

Seeking guidance on a perplexing issue. Here is a basic code snippet to depict my dilemma: html: <form> <input type="number" id="someNumber"> <input type="button" id="submitBtn"> </form> jquery: $("#submitBtn"). ...

Attempting to increase the size of the menu text upon hover action

It seems like a basic mistake on my part, but I just can't seem to make it work. What I'm trying to achieve is that when you hover over a specific item in the menu, the text within that item should increase in size. However, currently it only en ...

Selenium - Issue of Element Unavailability for Interaction

Seeking assistance with this issue. Despite researching all possible solutions, none have solved it. Attempting to interact with a button using Selenium, but encountering persistent errors: selenium.common.exceptions.ElementNotInteractableException: Messa ...

There was a SeleniumWebDriverException that occurred in the WebDriverdll when an exception with a null response was thrown while using EdgeDriver

I am currently working with Selenium in C# using the Edge driver. However, when I execute the code, I encounter the following error message. class HomePageTests { static void Main(string[] args) { { { IWebDriver ...

Flask is having trouble loading images within the static directory

I am facing an issue with loading two images in my HTML when the 'next' button is clicked. Initially, everything was working fine before I integrated the front end with Flask. The CSS and JS files are functioning properly, but the images are not ...

Guide on inserting text into a text box located within a Shadow DOM on a webpage with either Protractor or Selenium

Is it possible to input text into a text-box that is located under Shadow-root on a web page using protractor or selenium? Any tips or suggestions would be greatly appreciated. I am currently facing the challenge of entering values in a text-field with x ...