【问题标题】:how to click this outer div element(or any other span inside it) in selenium如何在 selenium 中单击此外部 div 元素(或其中的任何其他跨度)
【发布时间】:2018-02-28 10:45:32
【问题描述】:

我希望单击此 div,其中还包含许多跨度。每次加载页面时,外部 div 的 xpath 都会不断变化。那么有没有其他方法(除了xpath)来点击这个div?

我试过了-

Driver().findElement(By.cssSelector(".z3tile.z3activeTile.z3-tile-medium")).click();

但这没有用。

如果有人可以提出其他方法,将不胜感激。

<div class="z3tile z3activeTile z3-tile-medium"
bo-style="{'backgroundColor': getTileBackgroundColor(app) }"
bo-class="'z3-tile-'+preferences._View._TileSize"
onkeypress="audience(app._Code);openApp(event,
$(this).prev('div').attr('data-attr-param_keypress_url'),
$(this).prev('div').attr('title'),
$(this).prev('div').attr('data-attr-param_keypress_name'),
$(this).prev('div').attr('data-attr-param_keypress_ticket'),
$(this).prev('div').attr('bo-attr-param_keypress_langue'),
$(this).prev('div').attr('data-attr-param_keypress_paramBrowser'))"
ng-click="audience(app._Code);openApplication(app._Url, app._Source,
app._Name, app._Ticket, preferences._Langue, app._ParamBrowser)"
ng-hide="app._Search" role="link" tabindex="3" indexapp="4"
idgroup="-1" display-app-tooltip="" style="background-color: rgb(246,
131, 0);" data-original-title="" title="">

<span class="ng-binding z3appTitle-medium" type="text"
bo-class="'z3appTitle-'+preferences._View._TileSize"
bo-style="{'color': DefaultTitleColor + ' !important',
'backgroundColor':
getTitleTileBackgroundColor(getTileBackgroundColor(app))}"
ng-bind="app._ShortName" style="color: rgb(255, 255, 255);
background-color: rgb(144, 77, 0);">SAV2000</span>

<span class="ng-binding z3appRole-medium" type="text"
bo-class="'z3appRole-'+preferences._View._TileSize"
bo-style="{'color': DefaultRoleColor + ' !important'}"
bo-hide="preferences._View._TileSize == 'small'"
ng-bind="app._ShortRole" style="color: rgb(0, 0, 0);">OCEANE - Gestion
des Tickets d'Incidents et Module Alerte</span>

<span class="app-event changeOpacity ng-hide icon-event-red"
data-original-title="Application unavailable" data-placement="bottom"
data-container="body" type="text" ng-click="showModalAppEvent(app)"
ng-class="getEventOnAppClass(app)"
ng-show="showEventOnApp(app)"></span> <span class="z3appBasicat"
bo-style="{'color': DefaultRoleColor + ' !important'}"
bo-bind="showTileCode(app._Code)" style="color: rgb(0, 0,
0);">21W</span> </div>

【问题讨论】:

  • html的哪一部分发生了变化? class 内的 &lt;div&gt; 属性?它变成了什么?也许有一些静态部分可能需要寻找?
  • 你能用更多的 outerHTML 更新问题吗?
  • 你不能给div一个固定的id吗?您可以通过 id 检索,通常这是固定元素的最佳选择。

标签: java html selenium


【解决方案1】:

尝试使用 JavascriptExecutor 和 cssSelector 来单击元素,

参考代码,

WebElement element = driver.findElement(By.cssSelector(".z3tile.z3activeTile.z3-tile-medium"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

要了解有关JavascriptExecutor 的更多信息,请参阅此answer

【讨论】:

  • 谢谢阿米特。你能解释一下正常点击与这个 javaexecutor 有何不同吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-27
  • 1970-01-01
  • 2016-08-11
  • 2020-08-05
  • 2012-09-25
  • 1970-01-01
相关资源
最近更新 更多