这是我使用Selenium的第一天,我的网络知识很少,所以请容忍我。我的团队正在开发一个网站,我需要为它编写测试。该网站正在做的是,它将显示几个图表的一些数据,我们已经收集。我将在每个图表中搜索特定的单词,以确定图表是否正确显示,并提醒哪些没有正确显示。
所以我的问题是我不知道如何单独访问每个“图表对象”。我可以表演:
verifyTextPresent, target =, value = Good value只要6个图表中有一个正确显示并具有“良好价值”,该命令就能工作(在Selenium中是绿色的)。我认为它正在搜索整个网页,这就是为什么它能工作。如果我想单独测试每个图表,那么我就有问题了。
我尝试过的事情:
1)我尝试右键单击图表对象,Selenium表示目标名为
css=svg > rect
然而,它一直在失败。日志上写着
[error] Element css=svg > rect not found2)我去ide.jsp#locating-elements看看他们有什么窍门。不幸的是,这些例子并不完全符合我们正在做的事情,所以我随机地尝试了一些事情,这也不起作用(我的错)。我将在下面发布源代码html,这样做更有意义,但以下是我尝试过的一些内容:
id=numRecs
identifier=charts-6
css=class#charts-container//chart-6我做了一个搜索,上面的值是同类的,所以解析时不应该有任何混淆。
以下是Chrome中图表对象的部分来源。如果需要的话,我可以发布更多,但我认为这是关键的行,因为当我在这两个div对象上滚动鼠标时,图表会在Chrome中高亮显示:
<body>
blah blah
<div id="numRecs" data-chart="3">
<div class="charts-container" id="charts-6" style="position: relative; overflow: hidden; width: 1267px; height: 300px; text-align: left; line-height: normal; z-index: 0; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helvetica, sans-serif; font-size: 12px;">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="1267" height="300">
blah blah
<div id="another chart" data-chart="4">
blah blah
</div>
<div id="misc chart" data-chart="5">
blah blah
</div>
</body>希望有人能给我指明正确的方向。提前谢谢你的帮助。
发布于 2014-05-05 19:46:23
您可以使用xpath或css找到元素。我在JUnit中使用了(Eclipse),下面是一个用xpath查找元素的示例代码:
@Test
public void test1() throws Exception {
WebDriver driver = new FireFoxDriver();
String url = "example.html";
driver.get(url);
Thread.sleep(5000);
WebElement anotherChart = driver.findElement(By.id("another chart")); // find element by id
String anotherChartText = anotherChart.getText(); // Use getText to check data ("blah blah")
WebElement miscChart = driver.findElement(By.cssSelector("#another chart")); // find element with css
String attribute = miscChart.getAttribute("data-chart"); // Get the specified attribute of the WebElement ("5")
WebElement chart6 = driver.findElement(By.xpath("//div[@id='charts-6']")); // find element with xpath
WebElement numRec = driver.findElement(By.xpath("//div[@id='numRecs']")); // find element with xpath
WebElement numRecChart6 = numRec.findElement(By.xpath("div[@id='charts-6']")); // You can use WeElement.findElement() method to find an object in the WebElement
driver.close();
}我认为xpath和css对您很有帮助。要检查代码,可以使用xpath测试人员:http://www.freeformatter.com/xpath-tester.html#ad-output
https://stackoverflow.com/questions/23439370
复制相似问题