I am encountering issues with Selenium not functioning properly on Windows 10 when using the syntax driver.findElement(By.cssSelector

I'm encountering problems with the CSS selector while using Selenium on Windows 10. It appears that the tag is incorrect. How can I resolve this issue?

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.By.ById;
import org.openqa.selenium.By.ByXPath;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class Locator2 {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "C:\\Users\\abhij\\Desktop\\seliniumjars\\chromedriver.exe");

        WebDriver driver = new ChromeDriver();
        driver.get("https://login.yahoo.com/?.src=ym&.intl=us&.lang=en-       US&.done=https%3a//mail.yahoo.com");
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
        
        //driver.findElement(By.xpath(".//*[@id='login-username']")).sendKeys("asdfasd");
        driver.findElement(By.cssSelector("input[id='login-username']]")).sendKeys("asdfasd");

        //driver.findElement(By.cssSelector("input[id='login1']")).sendKeys("asdfasd");
        //driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
        //driver.findElement(By.cssSelector("input[name='login1']")).sendKeys("asdfasd");
    }
}

Error Message:

Error in thread "main" org.openqa.selenium.InvalidElementStateException: invalid element state: Failed to execute 'querySelector' on 'Document': 'input[id='login-username']]' is not a valid selector

Answer №1

There was an issue in the main thread that resulted in the error message: "org.openqa.selenium.InvalidElementStateException: invalid element state: Failed to execute 'querySelector' on 'Document': 'input[id='login-username']]' is not a valid selector"

The error message is accurate as there is a mistake in the cssSelector provided. Simply remove the extra square bracket at the end and try the corrected version below:

driver.findElement(By.cssSelector("input[id='login-username']")).sendKeys("asdfasd");

An alternative method is using the #id CSS selector to identify an element by its id attribute value with the following cssSelector:

driver.findElement(By.cssSelector("input#login-username")).sendKeys("asdfasd");

For more information on CSS selectors, you can refer to this resource.

Selenium also provides the option to locate elements directly by their id attribute value using By.id() as shown below:

driver.findElement(By.id("login-username")).sendKeys("asdfasd");

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

Tips for storing browser network logs in WebDriver

My current setup consists of: firefox version 58.0.1 geckodriver version 0.11.1 selenium-c# r.2.53.0 I am attempting to capture and save browser network logs using webdriver by configuring the firefoxProfile.SetPreference method. Can anyone provide guida ...

The preceding sibling function may encounter issues when multiple child tags are present

In this case, the XPath used to locate the checkbox is as follows: //td[text()='bbbb vvvvvvvvv']/preceding-sibling::td/div/input[@class='hidden'] However, despite using this XPath, the input element is not being captured. Only the "//t ...

Custom 505 handler for Railo and efficient ajax routing

I am struggling with a situation involving an external javascript file that includes a setinterval function to monitor file transfer completion between a server and a remote computer via a cfc. Strangely, the function works fine without a custom error hand ...

Locate, choose, and interact with a specific choice using Python Selenium

While working on an automated task software, I have been utilizing Selenium with Python to navigate the web. Despite making progress, I have encountered a problem that I am unable to resolve. I am trying to locate a tag using Selenium in order to choose o ...

The UnknownError in Selenium WebDriver has been triggered due to an unexpected issue where the function attribute_value.lastIndexOf is not recognized

My cucumber test using selenium-webdriver is encountering an issue. Upon running my feature, the test fails with Selenium::WebDriver::Error::UnknownError: unknown error: attribute_value.lastIndexOf is not a function [5] pry(#<Object>)> fill_in & ...

Steps for initiating the application under test for each test case without the need to generate a new session with WinAppDriver

Currently working on automating a Windows application using Java with WinAppDriver. I have set up three test cases, each requiring the application to be launched. Below is the code snippet I am using to initialize the driver and launch the application: D ...

How can I verify text modifications of an element using WebDriver and page objects?

Having some trouble with this topic, I'm stuck on how to assert that a text is shown after clicking a button in my page object class: By job = By.xpath("//input[@id='job0']"); public Page triggerJob() { wait.until(ExpectedConditions.pr ...

I cannot seem to understand why I keep encountering this error despite having added the commons-lang jar to my referenced libraries

I recently downloaded Java Faker library from here and proceeded to unzip it. After executing maven package, I found the JAR file for Faker in the target folder and added it to the build path. Although Selenium includes commons-lang, I am encountering a ...

Unable to click on the search button due to an advertisement blocking it

Recently, I encountered an issue while running a Python script with Selenium to perform a search on a webpage using the available search box. The problem arises when an advertisement pops up upon loading the webpage through my script, hiding the search box ...

Wicket scrambles the encoding of strings when passed from Javascript

When working with a Wicket component that involves JavaScript, I encounter an issue where the string I send back to Wicket contains special characters: "FICHIERfichier&é'(-è_çà)=~#{[`^@]}^$ù,;!¨£%µ§êë-+¤.0²123456789.pdf" To prevent ...

Check for length validation including spaces in JavaScript

My code includes a functionality that calculates the length of characters in a text area using both JSP and JavaScript: <textarea class="textAreaLarge" id="citation" rows="20" cols="180" name="citation" ...

Is there a way to retrieve the drop down selection from the Property file using Selenium WebDriver?

Presently engaged in working with Selenium WebDriver and utilizing Java. The reports are being generated within the TestNG framework. Currently scripting for a web application that consists of numerous drop-down menus, each offering multiple options. I hav ...

having difficulty clicking the "login" button during testing on Internet Explorer

During testing with IE, the page is opened and the fields for username and password are filled, but the login button is not being clicked. I am having trouble pinpointing the issue. System.setProperty("webdriver.ie.driver",AbsoluteDriverPath); webdri ...

Selenium WebDriver encountering TimeoutException when calling getScreenshotAs()方法

This is the code I am working on. public static void executeTest() throws IOException { System.setProperty("webdriver.chrome.driver", "data/chromedriver.exe"); driver = new ChromeDriver(); driver.manage().timeouts().pageLoadTimeout(30, TimeUni ...

Adding a PDF generated from an HTML div to an email using Java

I am looking for a solution to convert the contents of an HTML div tag into a PDF file while preserving its associated CSS. This is essential as I will be using Java on the back-end to send emails, and I need to attach the PDF with the CSS intact when send ...

Import a large JSON file into either a MySQL or Oracle database

My team at work is responsible for supplying files to other services. These files typically range in size from 5MB to 500MB. We are considering using JSON instead of XML, but I am concerned about how our customers will be able to upload these files into th ...

Verify if the SelectBox contains any options using Robot Framework and Selenium2Library

Is there a way to verify if a <select> dropdown contains more than one <option>? The list of <option>s is fetched dynamically via ajax and organized into two separate <optgroup> sections. <option value="">default</option& ...

Are the values for the edge driver experiencing any modifications?

We've been using a reliable framework for years now. When launching Edge (previously known as IE), we set the unexpectedAlertBehavior to: eOp.setCapability("unexpectedAlertBehaviour", "ignore"); ... driver = new EdgeDriver(eOp); E ...

Utilizing Selenium and Java to Alter the Source Code of a Web Page

I am dealing with a page that has source code structured like this: <header style="position: fixed;"> Unfortunately, when I try to use my Java script, clicking on an element becomes impossible because it is located underneath the header. This resul ...

What steps should I follow to run tests with Maven while ensuring that the features are ordered correctly?

Currently, my integration tests with an e-commerce application are being run using the combo of Maven + Selenium + Jenkins. An issue arose where Maven does not adhere to the order of the .feature files: 1-test_case.feature 2-test_case.feature 3-test_ca ...