首页 > 解决方案 > Selenium 没有通过它的 xPath 找到元素

问题描述

试图通过其 xPath 从 Facebook 上传一些图像,没有幸运,还有其他方法吗?

在此处输入图像描述

爪哇:

var xPathContato = "//*[@id=\"mount_0_0_lv\"]/div/div[1]/div/div[3]/div/div/div[1]/div[1]/div/div/div[4]/div/div/div/div/div/div/div/div/div[3]";
WebElement wb = webDriver.findElement(By.xpath(xPathContato));

String image = wb.getAttribute("src");
 
URL imageURL = new URL(image );
BufferedImage saveImage = ImageIO.read(imageURL);
 
ImageIO.write(saveImage, "png", new File("facebook-img"));

错误:

Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="mount_0_0_lv"]/div/div[1]/div/div[3]/div/div/div[1]/div[1]/div/div/div[4]/div/div/div/div/div/div/div/div/div[3]"}

标签: javaseleniumfacebookselenium-webdriver

解决方案


我认为你在混淆一些东西。该元素并没有真正退出您正在上传到 selenium 的页面中,否则它会毫无问题地显示出来,但该元素基本上只存在于浏览器的开发人员工具栏中。


推荐阅读