Yury Staravoitau
Yury Staravoitau

Reputation: 175

How to verify what type of web element (select box or text)

I'm using Page Factory for automation and I want to use the following code:

@FindBy(how = How.XPATH, using = "//div[contains(text(),'sometext')]")
private WebElement _selectBox1;

But I do not know how to use xpath for this situation, because this web element could be as select box but sometimes this web element could be as text.

Element on the page can be as select box (if some values are available for product)

<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>

if the database is not present the data for the product, it is displayed as text:

<div class="feature">
<span class="ynIco noIco"/>
<strong>Not available</strong>
</div>

Upvotes: 1

Views: 1499

Answers (2)

Sergey Syrisko
Sergey Syrisko

Reputation: 26

@FindBy(how = How.XPATH, using = "//div[contains(text(),'sometext') or count(./select)=1]")
private WebElement _selectBox1;

Upvotes: 1

Santoshsarma
Santoshsarma

Reputation: 5667

Check whether the text box is present or not.

  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.
  }

See this for isElementPresent() implementation.

Upvotes: 0

Related Questions