如何基于Selenium深处的几个标签找到一个元素

时间:2016-07-29 17:59:56

标签: html selenium

所有,我,对Selenium和一般编程都很新。我坚持自动化网站。我需要一些帮助,我可以找到并点击一个元素,根据文本中的几个标签进入html代码。

我正在寻找以下HTML代码中包含文本TEXT DISPLAYED IN BROWSER的元素。我可以找到元素,如果我使用&#34;按id找到元素&#34;上 <div id="appt-24175027" class="grid-row pointer">,但问题是ID appt-24175027动态变化。

在HTML代码中,我正在寻找以下内容......

1st ELEMENT/TEXT DISPLAYED IN BROWSER
2nd ELEMENT / TEXT DISPLAYED IN BROWSER
3rd ELEMENT / Text DISPLAYED IN BROWSER

有人能指出我正确的方向吗?

HTML:

<script type="text/javascript">
<script type="text/javascript">
<div id="content" class="content">
<div class="grid-container started">
<div id="started-block" class="grid-header pointer">
<div class="grid-header-icon">
<div class="icon-started">
<div class="inner"></div>
</div>
</div>
<div id="appt-24175027" class="grid-row pointer">
<div class="cell cell-content">
<div class="main-field">
<span style="color:#000000">TASK ACTIVE</span>
</div>
</div>
</div>
</div>
</div>
<div class="grid-container pending expanded">
<div id="pending-block" class="grid-header pointer">
<div class="grid-expander">
<div class="inner"></div>
</div>
<div class="grid-header-icon">
<div class="icon-pending">
<div class="inner"></div>
</div>
</div>
<div class="grid-header-title">Pending (5)</div>
</div>
<div id="pending-grid" class="grid-body">
<div id="appt-24181299" class="grid-row pointer">
<div class="cell appt-color" style="border-top: 1px solid rgb(255, 244,          0);border-right: 1px solid rgb(204, 177, 0); background: #FFDE00;"></div>
<div class="cell cell-content">
<div class="main-field">
<span style="color:#000000"></span>
</div>
</div>
</div>
<div id="appt-24180104" class="grid-row pointer">
<div class="cell appt-color" style="border-top: 1px solid rgb(255, 244,   0);border-right: 1px solid rgb(204, 177, 0); background: #FFDE00;"></div>
<div class="cell cell-content">
<div class="main-field">
<span style="color:#000000"> 1st ELEMENT/TEXT DISPLAYED IN BROWSER</span>
</div>
</div>
</div>
<div id="appt-24175024" class="grid-row pointer">
<div class="cell appt-color" style="border-top: 1px solid rgb(255, 224,     168);border-right: 1px solid rgb(204, 163, 122); background: #FFCC99;"></div>
<div class="cell cell-content">
<div class="main-field">
<span style="color:#000000"> 2nd ELEMENT / TEXT DISPLAYED IN BROWSER</span>
</div>
</div>
</div>
<div id="appt-24175025" class="grid-row pointer">
<div class="cell appt-color" style="border-top: 1px solid rgb(255, 224, 168);border-right: 1px solid rgb(204, 163, 122); background: #FFCC99;"></div>
<div class="cell cell-content">
<div class="main-field">
<span style="color:#000000">3rd ELEMENT / Text DISPLAYED IN BROWSER</span>
</div>
</div>
</div>
<div id="appt-24175026" class="grid-row pointer">
</div>

1 个答案:

答案 0 :(得分:0)

使用xpath,您只需使用内部文本找到<div class="grid-row pointer">,如下所示:

//div[@class = 'grid-row pointer' and contains(.,'1st ELEMENT/TEXT DISPLAYED IN BROWSER')]

//div[@class = 'grid-row pointer' and contains(.,'2nd ELEMENT/TEXT DISPLAYED IN BROWSER')]

//div[@class = 'grid-row pointer' and contains(.,'3rd ELEMENT/TEXT DISPLAYED IN BROWSER')]

希望它有所帮助.. :)