Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Selenium访问Iframe中的元素_Java_Selenium_Xpath - Fatal编程技术网

Java 使用Selenium访问Iframe中的元素

Java 使用Selenium访问Iframe中的元素,java,selenium,xpath,Java,Selenium,Xpath,我试图通过XPath访问一个元素,但不知何故它无法工作。元素位于IFrame中,这可能是原因吗? 如果是,我需要做什么才能仍然访问它? 或者可能是因为我嵌套了两个html(html代码图片)?有什么解决办法 我收到的错误消息是: org.openqa.selenium.NoSuchElementException:无法定位元素:{“方法”:“xpath”,“选择器”:“/*[@id='oslist']/table/tbody/tr/td/table/tbody/tr/td/a”} 命令持续时间或

我试图通过XPath访问一个元素,但不知何故它无法工作。元素位于IFrame中,这可能是原因吗? 如果是,我需要做什么才能仍然访问它? 或者可能是因为我嵌套了两个html(html代码图片)?有什么解决办法

我收到的错误消息是:

org.openqa.selenium.NoSuchElementException:无法定位元素:{“方法”:“xpath”,“选择器”:“/*[@id='oslist']/table/tbody/tr/td/table/tbody/tr/td/a”}
命令持续时间或超时:1.14秒

Java代码:

 @Test
 public void testGenerierter() throws Exception {

     driver.findElement(By.id("cmdSigOn")).click();
     driver.findElement(By.id("k278")).click();
     driver.findElement(By.id("k279")).click();
     driver.findElement(By.id("k332")).click();
     Thread.sleep(3000);
     driver.switchTo().frame("1");

     Thread.sleep(3000);
     driver.findElement(By.xpath(".//*[@id='oslist']/table[1]/tbody/tr[2]/td[2]/table/tbody/tr[1]/td/a"));
     driver.findElement(By.id("LogoutButton")).click();

}

试试这个:

driver.switchTo().frame(0);

您正在传递一个整数,因此不需要双引号。并尝试验证正确的iframe索引号和下面的xpath。

您在xpath上遇到异常,这意味着您在第
行的driver.findElement(By.xpath(“./*[@id='oslist']]/table[1]/tbody/tr[2]/td[2]/table/tbody/tr[1]/td/a”)

}


PS:为什么要使用绝对xpath。相对xpath是最佳实践

好了,伙计们,这就是解决方案。我使用Webelement的方法

driver.switchTo().frame(driver.findElement(By.name("1")));

我试过了,但还是同样的错误。它找不到0或1的框架你能提供实际的网站地址吗?我尝试了绝对路径和相对路径。使用插件firepath生成Firefox的路径。我尝试了absolut,因为我已经分离了Iframes。您可以共享查找xpath或URL的代码的屏幕截图。我用查找xpath的图片调整了我的问题
driver.switchTo().frame(driver.findElement(By.name("1")));