我正在努力验证 html 对象标记内的 html 元素。
以下是我的测试 html (test1.html
)。
<html>
<body>
<h1>this is test 1</h1>
<object id='obj1' width="100%" height="200px" data="http://www.w3schools.com/"></object>
<br> <br> <br> <br>
<iframe id='ifr1' width="100%" height="200px" src="http://www.w3schools.com/"></iframe>
</body>
</html>
如果是 iframe
,使用 webdriver switchto frame 方法很容易处理,如下例所示。
@Test
public void testIframe() {
try {
FirefoxDriver driver = new FirefoxDriver();
driver.get("http://localhost/test1.html");
driver.switchTo().frame(0);
Assert.assertTrue(driver.findElement(By.xpath("/html/body/div[1]/div")).getText().equals("THE WORLD'S LARGEST WEB DEVELOPER SITE"));
driver.quit();
} catch (Exception e) {
e.printStackTrace();
}
}
但是在对象标签的情况下,switchTo
方法不能以相同的方式应用。
我很好奇 Selenium 是否提供这种处理方式。
有没有人有解决这个问题的想法,或者有什么建议?
最佳答案
您也可以使用 SwitchTo api 以这种方式访问对象元素:
driver.SwitchTo().Frame(driver.FindElement(By.TagName("object")));
关于java - Selenium 处理对象标签内的 html 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33000001/