我正在使用Page Factory进行自动化,我想使用以下代码:
@FindBy(how = How.XPATH, using = "//div[contains(text(),'sometext')]")
private WebElement _selectBox1;
但我不知道如何在这种情况下使用xpath,因为这个web元素可以作为选择框,但有时这个web元素可以作为文本。
页面上的元素可以作为选择框(如果某些值可用于产品)
<div>
<select id="id_2" class="selectBox" onchange="OnsSelectHandler(this,2)" style="display: none;">
<option value="">Click to select</option>
<option value="3341">value 1</option>
<option value="3342">value 2</option>
</select>
</div>
如果数据库不存在产品的数据,则显示为文本:
<div class="feature">
<span class="ynIco noIco"/>
<strong>Not available</strong>
</div>
答案 0 :(得分:1)
@FindBy(how = How.XPATH, using = "//div[contains(text(),'sometext') or count(./select)=1]")
private WebElement _selectBox1;
答案 1 :(得分:0)
检查文本框是否存在。
if(isElementPresent(By.cssSelector("div.feature > span")))
{
If it is present, then no need to think about select box (Pick list values)
}
else
{
Do your operation with select box.
}
有关isElementPresent()的实现,请参阅this。