简体   繁体   English

Selenium Webdriver:cssselector

[英]Selenium Webdriver: cssselector

I am trying to do SignIn for this . 我试图来为他们做登入 In it click on 'SignIn' which I have done it successfully. 在其中点击我已成功完成的'SignIn'。 Now when trying to type in Username/Password using Xpath it shows exception which says 现在,当尝试使用Xpath键入用户名/密码时,它会显示异常

Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: element not visible 线程“main”中的异常org.openqa.selenium.ElementNotVisibleException:元素不可见

code is :- 代码是: -

driver.findElement(By.xpath(".//*[@id='load_form']/fieldset[1]/input")).sendKeys("test123");
driver.findElement(By.xpath(".//*[@id='load_form']/fieldset[2]/input")).sendKeys("test123");

I know this Xpath is same as used in SignUp form, so what are the other ways to locate these two fields? 我知道这个XpathSignUp形式中使用的相同,那么找到这两个字段的其他方法是什么? How we can use it by using cssselector ? 我们如何使用cssselector来使用它? Thanks for the help in advance. 我在这里先向您的帮助表示感谢。

Go One Level up on finding the relative xpath with 在找到相对xpath时向上一级

//div[@id='login']/form[@id='load_form']//input[@name='username']
//div[@id='login']/form[@id='load_form']//input[@name='password']

Try this 尝试这个

 //For Username
        driver.findElement(By.xpath("(//input[@name='username'])[2]")).sendKeys("username test");
//or
        driver.findElement(By.cssSelector("#login > #load_form > fieldset > input[name='usernam']")).click();   

//For password
        driver.findElement(By.xpath("(//input[@name='password'])[2]")).sendKeys("password test");
//or
        driver.findElement(By.cssSelector("#login > #load_form > fieldset > input[name='password']")).click();

the above codes are working for me. 以上代码对我有用。

There are basically two elements found by provided xpath , and it works with first found element which is invisible as your exception saying, you should try using cssSelector as below :- 提供的xpath基本上有两个元素,它与第一个找不到的元素一起使用,作为你的异常说,你应该尝试使用cssSelector如下: -

driver.findElement(By.cssSelector("div#login input[name = 'username']")).sendKeys("test123");
driver.findElement(By.cssSelector("div#login input[name = 'password']")).sendKeys("test123");

Note :- For learning about cssSelector follow this url and for xPath follow this url 注意 : - 要了解cssSelector按照此URL操作 ,对于xPath遵循此URL

I'm suggesting you before selenium execution with the locator you need to sure that using locator is correct and returns single or multiple element at your browser console by pressing f12 . 我建议您在使用定位器执行selenium之前确保使用定位器是正确的,并通过按f12浏览器控制台上返回单个或多个元素。 for verify xPath you should use $x("your xpath expression") and for cssSelector you should use document.querySelector("your css selector expression") .. 对于验证xPath您应该使用$x("your xpath expression") ,对于cssSelector您应该使用document.querySelector("your css selector expression")

Hope it will help you..:) 希望它会帮助你.. :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM