【问题标题】:driver.findElement(By.cssSelector(..)) Selenium Not Working In Windows 10driver.findElement(By.cssSelector(..)) Selenium 在 Windows 10 中不起作用
【发布时间】:2020-09-06 02:12:27
【问题描述】:

Seleniumwindows 10 中的 CSS 选择器有问题。标签似乎不正确。而且,我不确定出了什么问题。你能帮忙吗?

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 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

【问题讨论】:

  • 无法使用 selinium 让 css 选择器工作,标签是正确的,不确定我错过了什么
  • 下面是我得到的输出
  • 线程“主”org.openqa.selenium.InvalidElementStateException 中的异常:无效元素状态:无法在“文档”上执行“querySelector”:“input[id='login-username']]'不是有效的选择器。
  • 您是否阅读了错误信息?它说什么?

标签: java selenium-webdriver error-handling css-selectors windows-10


【解决方案1】:

线程“main”org.openqa.selenium.InvalidElementStateException 中的异常:无效元素状态:无法在“Document”上执行“querySelector”:“input[id='login-username']]' 不是有效的选择器

错误是绝对正确的,因为您的cssSelector 不正确,只需省略最后一个额外的] 方括号并尝试如下:-

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

您也可以使用#id css selecter 来定位具有id 属性值的元素,使用cssSelector 如下:-

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

To learn more about css selector follow this reference.

Selenium 也可以直接使用元素的id 属性值定位元素,因此您可以使用By.id() 以及以下方式定位该元素:-

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

【讨论】:

  • 谢谢 saurabh.. 明白了
猜你喜欢
  • 1970-01-01
  • 2020-07-18
  • 2016-03-08
  • 2018-12-04
  • 2015-12-12
  • 2018-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多