java Selenium - driver.findElement(By.cssSelector(..)) 在 Windows 10 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40072671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium - driver.findElement(By.cssSelector(..)) not working in windows 10
提问by Abhijith Reddy
css selectore in Selenium
not working on windows 10
, tag is correct not sure can u please help
css选择器Selenium
无法正常工作windows 10
,标签是否正确不确定你能帮忙吗
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");
}
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
线程“main” org.openqa.selenium.InvalidElementStateException 中的异常:元素状态无效:无法在“文档”上执行“querySelector”:“input[id='login-username']]' 不是有效的选择器
回答by Saurabh Gaur
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
线程“main” org.openqa.selenium.InvalidElementStateException 中的异常:元素状态无效:无法在“文档”上执行“querySelector”:“input[id='login-username']]' 不是有效的选择器
Error is absolutely correct, because your cssSelector
is incorrect, just omit last ]
square bracket which is extra and try as below :-
错误是绝对正确的,因为您cssSelector
不正确,只需省略最后一个]
额外的方括号,然后尝试如下:-
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 :-
您还可以使用以下#id css selecter
方法定位具有id
属性值的元素cssSelector
:-
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 :-
Selenium
还id
可以直接使用元素的属性值定位元素,因此您可以使用By.id()
以下方法定位该元素:-
driver.findElement(By.id("login-username")).sendKeys("asdfasd");