0

我需要从 UI 列表中选择元素APPLICATION 。这个元素的html代码(在html中我有10个这样的元素)是:

<tr id="4676856" class="menuItem" orientation="vertical" collectionid="tr4" radioid="12">
<td id="ItemL" class="left" data-click="[["runScript",["switchApplication('myapp')"]]]" data-ctl="nav">
<div class="menuRB"/>
</td>
<td id="ItemM" class="middleBack" tabindex="0" data-click="[["runScript",["switchApplication('myapp')"]]]"     data-ctl="nav">**APPLICATION**</td>
<td id="ItemR" class="rightEdge" data-click="[["runScript",["switchApplication('myapp')"]]]" data-ctl="nav"/>

我从未使用过这样的脚本,如何使用 selenium webdriver 找到这样的元素?

我努力了

driver.findElement(By.xpath(".//*[@id='yui-gen0']/div[6]/table/tbody/tr[12]/td[1]")).click();
4

1 回答 1

1

Please show HTML code that matches your XPaths (where is yui-gen0 and such?)

From the info you provided, few issues you might want to avoid:

  • Try avoid using div[6], tr[12], use meaningful things instead of index
  • Don't use yui-gen0 if it's auto-generated
  • Keep HTML ids unique, like id="ItemM". If they are unique, use them directly

I'd suggest try the following (we need you to show more HTML in order to avoid using div[6], tr[12]):

// find by text '**APPLICATION**'
driver.findElement(By.xpath(".//*[@id='yui-gen0']/div[6]/table/tbody/tr[12]/td[text()='**APPLICATION**']")).click();

// find by class name (if it's the only one)
driver.findElement(By.xpath(".//*[@id='yui-gen0']/div[6]/table/tbody/tr[12]/td[@class='middleBack']")).click();

// find by class name (if there are others)
driver.findElement(By.xpath(".//*[@id='yui-gen0']/div[6]/table/tbody/tr[12]/td[contains(@class, 'middleBack')]")).click();

// find by id, which should be unique, if it's not, your HTML is bad
driver.findElement(By.xpath(".//*[@id='ItemM']")).click();
于 2013-11-04T22:03:52.267 回答