html - 如何从 Selenium 中的网页访问对象

标签 html css selenium selenium-ide

这是我第一天使用 Selenium IDE,我的网络知识很少,所以请多多包涵。 我的团队正在开发一个网站,我需要为其编写测试。该网站正在做的是它会显示我们收集的一些数据的几个图表。我将在每个图表中搜索特定单词以确定图表是否正确显示并提醒哪些图表显示不正确。

所以我的问题是我不知道如何单独访问每个“图表对象”。我可以执行:

verifyTextPresent, target =, value = Good value

只要 6 个图表中的一个正确显示并且其中具有“良好值(value)”,该命令就会起作用(在 Selenium IDE 中为绿色)。我认为它正在搜索整个网页,这就是它起作用的原因。如果我想单独测试每个图表,那我就有问题了。

我尝试过的事情:

1) 我尝试右键单击图表对象,Selenium IDE 说目标被调用

css=svg> 矩形

然而,它总是失败。日志说

[error] Element css=svg > rect not found

2) 我去了http://docs.seleniumhq.org/docs/02_selenium_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>

希望有人能指出我正确的方向。预先感谢您的帮助。

最佳答案

您可以使用 xpath 或 css 查找元素。我在 Java (Eclipse) 中将 Selenium 与 JUnit 结合使用,这里是使用 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

关于html - 如何从 Selenium 中的网页访问对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23439370/

相关文章:

javascript - 更改通过谷歌标签管理器添加并在 iframe 中异步加载的小部件的 css 属性

javascript - HTML5 使用 insertBefore 进行拖放

html - em 的响应尺寸

css - 如何以纯 CSS 方式使 100% 宽度的 div 始终具有与其宽度的 50% 相对高度?

css - 在 Sublime 中搜索所有字体系列 : not preceded by an opening comment 的正则表达式

javascript - Google+ signinCallback 调用了两次并且在处理过程中丢失了 authresult

javascript - 多个秒表无法正常工作

python - Selenium:模拟人类打字行为

Selenium CSS 定位器用于嵌套 div 内的跨度文本

python - 在 python/ruby/perl 中使用 Selenium Webdriver