【发布时间】:2011-09-16 08:15:19
【问题描述】:
我正在使用 webdriver 阅读来自 gmail 的邮件,在这期间我发现了 By.id 和 By.tagname 之间的这种差异。
我正在尝试访问 id 为“:pg”的“表”。所以我可以
- 要么使用 By.id(":pg")
- 或使用 By.tagname("table") 并搜索 id 为 :pg 的元素
这是两种情况的代码。
通过.id:
WebDriver webDriver = new FirefoxDriver();
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver = webDriver.switchTo().frame("canvas_frame");
WebElement table1 = webDriver.findElement(By.id(":pg"));`
上面的代码,我直接得到了id为“:pg”的元素
通过.tagname:
WebDriver webDriver = new FirefoxDriver();
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
List<WebElement> tables = webDriver.findElements(By.tagName("table"));
for(WebElement table2: tables){
String id = table2.getAttribute("id");
System.out.println("id: "+ id);
if(id != null && id.equals(":pg")){
System.out.println("FOUND IT!!!");
}
}
在上面的代码中,我找到所有带有 table 标记名的元素,然后查看哪个元素的 id 为 ":pg"。
这两个代码 sn-ps 本质上是相同的,但使用不同的方式(By.id 或 By.tagname)。但是,使用 By.id 的代码的第一个 sn-p 总是成功,而使用 By.tagname 的代码的第二个 sn-p 几乎总是失败。 (但它会在额外的等待下工作)
为什么 By.id 和 By.tagname 之间存在这种差异?
谢谢, 克里斯。
【问题讨论】:
标签: testing automation automated-tests webdriver