I'm having issues with CSS Selector in Selenium and windows 10. Tag seems to be incorrect. And, I'm not sure what's wrong. Can you please help?
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(" 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"); } } Exception:
>Exception 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 41 Answer
Exception 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
Error is absolutely correct, because your cssSelector is incorrect, just omit last ] square bracket which is extra and try as below :-
driver.findElement(By.cssSelector("input[id='login-username']")).sendKeys("asdfasd"); You can use also #id css selecter to locate an element with their id attribute value using cssSelector as below :-
driver.findElement(By.cssSelector("input#login-username")).sendKeys("asdfasd"); To learn more about css selector follow this reference.
Selenium also locate an element using id attribute value of an element directly, so you can locate this element using By.id() as well as below :-
driver.findElement(By.id("login-username")).sendKeys("asdfasd"); 1