Having trouble searching using CSS or XPath with Selenium Webdriver, but no issues when searching by ID or class

I am currently using Eclipse Mars along with JDK 1.8. Below is the code I have written:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class test1 {
    public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver", "D://chromedriver.exe");
WebDriver d1=new ChromeDriver();
WebElement e1; 
d1.get("https://www.google.co.in/");

The following line executes without any issues:

e1=d1.findElement(By.id("lst-ib"));

However, when I try to search using css or xpath, nothing is returned.

e1=d1.findElement(By.cssSelector("input[class='gsfi'][name='q'][id='lst-ib']"));

or

e1=d1.findElement(By.xpath("input[@class='gsfi'][@name='q'][@id='lst-ib']"));

Answer №1

When utilizing xpath, be sure to use the following code snippet:

 d1.findElement(By.xpath("//*[@id=\"lst-ib\"]"))

If you prefer to use css selector, then utilize this code:

d1.findElement(By.cssSelector("input#lst-ib.gsfi"))

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

Encountering a null hostname error while utilizing AWS Transcribe services

I've implemented the following code snippet: public class ProviderTranscribeController { private AmazonTranscribe client = AmazonTranscribeClient.builder().withRegion(Regions.EU_WEST_2).build(); final AmazonS3 s3 = AmazonS3ClientBuilder ...

Exploring Stackoverflow integration with Hibernate by leveraging the SQL IN clause with a list of IDs

Encountering a stack overflow error due to the large number of parameters in a SQL statement with IN (id, id, id...id). Is there a solution for this issue? This is specific to my local environment using Eclipse. JPA @Query(value="SELECT p FROM Pendin ...

The signature provided by the pusher is invalid: The expected HMAC SHA256 in hexadecimal digest is

The HTML file contains JavaScript code that calls the server for authentication. The code snippet from the HTML file is as follows: <html> <script> <head> var options = { authEndpoint: "api/pusher.json?socket_id=9900&channel_name ...

Is there a way to retrieve an optional instead of raising an exception?

Is it possible to handle an exception that may be thrown by my method by returning an optional instead of using a throws statement or try and catch block? The exception in question is an IO exception which I have no control over, rather than an error that ...

What steps do I need to take to make sure the Google font I downloaded is functioning properly in my React project

I have encountered an issue with using a Google font in my project... After downloading the font, I included the following CSS code: @font-face { font-family: "Work Sans"; src: local("Work Sans"), url(../src/Fonts/Work_Sans/Wor ...

How can I prevent certain applications from being included in the analysis performed by CodeCoverage.exe for IIS

Running my Jenkins setup on the local machine involves conducting integration tests using vstest.console.exe, with some involving Selenium. The process collects coverage analysis into a file, and for gathering coverage during Selenium tests, I utilize Team ...

Could you provide me with some information regarding the relationship between screen resolution and screen size?

When checking my website on different screen resolutions, I rely on a tool called Screenfly from quirktools.com. While using this tool, I came across an interesting feature - the display icon in the top left corner with a dropdown menu showing resolution ...

Preventing Selenium from clicking an element outside the viewport on iOS Safari

Having an issue with automating a webpage using the following technologies: C# iOS Safari The problem arises when trying to click on an image that is positioned 75% off the bottom of the page. Selenium sees the visible part of the image and attempts to ...

Seeking advice on how to successfully integrate a Primary Flight Display

As a developer skilled in Ada, C, Obj.C (iOS), and C#, I am looking to create an application to display data about a robot I am currently constructing. However, I have limited experience with Mac libraries or OpenGL when it comes to graphics. The desired ...

How can a CSS class be inherited from a different file?

I have defined a class for styling a button: .client-header button { /*Properties*/ } And I also have a class to indicate when the menu is open: .client-menu-open { /*Properties*/ } My goal is to change the background of the button depending on ...

Unable to get spacing correct on loading page

My attempt at creating a loading page using CSS and HTML has hit a roadblock. I'm trying to show a loading bar that ranges from 0% to 100%. Despite my use of justify-content: space-between, I can't seem to get it right. I've searched through ...

Tips for looping through select dropdowns with selenium

Essentially, I am faced with a series of droplists that require my interaction. https://i.sstatic.net/nvo4l.png I have successfully managed to interact with the initial droplist. from selenium import webdriver from selenium.webdriver.common.keys import K ...

Having issues with customizing the design of MaterialUI Accordion header

Link to my code sandbox project I am encountering issues with the styling in my Accordion Heading. My Accordion Contents are set up like this: <Accordion heading1= { <DishItem name="Döner Teller Spezial" price="13,60€"/> ...

Center an image and superimpose text in the middle of the webpage

I am attempting to center an image with text overlay in the middle of a page. Although the image is centered correctly, the overlay text remains off-center. Below is the code snippet. The text should be positioned in the top left corner of the image. &l ...

I am having trouble styling a pre-existing class in bootstrap using my custom CSS file

html <section id="title"> <div class="container-fluid"> <!-- Nav Bar --> <nav class="navbar navbar-expand-lg navbar-dark"> <a class=" navbar-brand">tindog</a> ...

Tips on placing a white background behind fa-3x (Font-awesome) icons

I am facing challenges while trying to customize the background behind my font-awesome icons. My goal is to create a white background that does not extend beyond the actual icon itself. Currently, the result looks like the image below: https://i.sstatic. ...

Tips for creating a hover effect on a rounded outline button with a gradient border

What I am looking for: I want the buttons to have rounded corners; The buttons should use linear-gradient, with button A as the border and button B as the background color; When hovering over the buttons, the opacity should change to 0.5. This works fine ...

Is there a way in C# Selenium specflow to automatically navigate to the newly opened page after clicking the register button?

As a beginner in C# Selenium, I encountered an issue with my test cases. After creating multiple steps on the main page and clicking the "register button" which redirects me to a new page, my WebDriver is still on the previous page causing my tests to fail ...

Tips for adjusting image size using media queries

How can I ensure that the image resizes correctly on smaller screens? Currently, the image spills over the container and there are gaps between the top/left of the container and the image. Should I adjust the image size in the media query or expand the con ...

Utilize Selenium to automate a webpage that is specifically compatible with Internet Explorer

My current challenge involves automating a webpage using Selenium that specifically requires Internet Explorer. Unfortunately, I am unable to use Edge Compatibility Mode and the Linux server I am working with does not support IE mode in Edge for Linux. I h ...