Gwt 我们如何访问SeleniumWebDriver(Java)中的实际元素?
在SeleniumWebDriver(Java)中 我们可以得到GWT元素Gwt 我们如何访问SeleniumWebDriver(Java)中的实际元素?,gwt,selenium,selenium-webdriver,gwt2,Gwt,Selenium,Selenium Webdriver,Gwt2,在SeleniumWebDriver(Java)中 我们可以得到GWT元素 例如,WebElement obj=driver.findElement(By.id(“gwt debug celltable”) 通过obj我们可以得到celltable的webelement,但我们不会得到实际的celltable。因此,如果您想使用SeleniumWebDriver检查celltable中的记录数。我需要做什么 可能吗?如果是,请尽快回答。是。您可以使用xpath实现这一点: List<Web
例如,
WebElement obj=driver.findElement(By.id(“gwt debug celltable”)代码>
通过obj
我们可以得到celltable的webelement,但我们不会得到实际的celltable。因此,如果您想使用SeleniumWebDriver检查celltable中的记录数。我需要做什么
可能吗?如果是,请尽快回答。是。您可以使用xpath实现这一点:
List<WebElement> elements =
driver.findElements(By.xpath("//table[@id='gwt-debug-celltable']/tbody/tr"));
列出元素=
findElements(By.xpath(“//table[@id='gwt-debug-celltable']/tbody/tr”);
在元素中,将显示行列表。我还没有测试过这段代码。但是它就像我们在项目中使用的一样。对于webtable,我参考了下面的链接
从下面的代码中,您可以从webtable中获取所有值
public class MaxFromTable {
public static void main(String[] args) throws ParseException {
WebDriver wd;
System.setProperty("webdriver.chrome.driver","G://chromedriver.exe");
wd= new ChromeDriver();
wd.get("http://money.rediff.com/gainers/bsc/daily/groupa?");
String max;
double m=0,r=0;
//No. of Columns
List col = wd.findElements(By.xpath(".//[@id='leftcontainer']/table/thead/tr/th"));
System.out.println("Total No of columns are : " +col.size());
//No.of rows
List rows = wd.findElements(By.xpath (".//*[@id='leftcontainer']/table/tbody/tr/td[1]"));
System.out.println("Total No of rows are : " + rows.size());
for (int i =1;i<rows.size();i++)
{
max= wd.findElement(By.xpath("html/body/div[1]/div[5]/table/tbody/tr[" + (i+1)+ "]/td[4]")).getText();
NumberFormat f =NumberFormat.getNumberInstance();
Number num = f.parse(max);
max = num.toString();
m = Double.parseDouble(max);
if(m>r)
{
r=m;
}
}
System.out.println("Maximum current price is : "+ r);
}
}
公共类MaxFromTable{
公共静态void main(字符串[]args)引发异常{
WebDriver-wd;
System.setProperty(“webdriver.chrome.driver”,“G://chromedriver.exe”);
wd=新的ChromeDriver();
wd.get(“http://money.rediff.com/gainers/bsc/daily/groupa?");
字符串最大值;
双m=0,r=0;
//栏数
List col=wd.findElements(By.xpath(“./[@id='leftcontainer']]/table/thead/tr/th”);
System.out.println(“总列数为:“+col.size()”);
//行数
List rows=wd.findElements(By.xpath(“./*[@id='leftcontainer']]/table/tbody/tr/td[1]”);
System.out.println(“总行数为:“+rows.size()”);
对于(int i=1;ir)
{
r=m;
}
}
System.out.println(“当前最高价格为:”+r);
}
}
如果您想查询GWT编译成Web驱动程序刚刚获得的HTML的java对象,答案可能是否定的。这就是单元测试的目的。为什么不直接按id查找元素呢?由于speedtr标记在gwt中没有id,因此xpath不是查找内容的最首选方法。当然,您可以通过id找到table元素并进行递归查找。但我认为xpath是一种更优雅的方式,或者您可以只在tr标记或类中添加ID,这将是更好的web设计。