【发布时间】:2017-07-13 11:15:30
【问题描述】:
我正在尝试使用 cssSelector 而不是 xpath。我打开了 CNN 网站,并把 Politics 的xpath 放在了顶部。然后尝试使用cssSelector 选择它。我无法弄清楚我写的cssSelector 有什么问题。我收到NoSuchElementException
线程 "main" org.openqa.selenium.NoSuchElementException 中的异常:没有这样的元素:无法找到元素:{"method":"css selector","selector":"*[id='nav'] div :nth-child(2) div:nth-child(2) a:nth-child(3)"}
这里是代码
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.get("http://cnn.com");
//driver.findElement(By.xpath("//*[@id='nav']/div[2]/div[2]/a[3]")).click();
driver.findElement(By.cssSelector("*[id='nav'] div:nth-child(2) div:nth-child(2) a:nth-child(3)")).click();
【问题讨论】:
-
能否请您发布日志
-
线程“main”中的异常 org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“method”:“css selector”,“selector”:“* [id = 'nav'] div:nth-child(2) div:nth-child(2) a:nth-child(3)"}
-
1.你真的有4个孩子吗? nth-child 编号从 0 开始,而不是 1
-
2.元素是由 JS 静态加载还是动态加载?
-
静态。 xPATH 不会改变。我尝试使用 nth-child 编号 0。 driver.findElement(By.cssSelector("*[id='nav'] div:nth-child(1) div:nth-child(1) a:nth-child(2 )“))。点击();我仍然得到错误。我要做的就是单击从 xPath 中给出的相同根元素中获取的政治部分
标签: java selenium selenium-webdriver css-selectors nosuchelementexception