1

I am using selenium with Java. I am unable to get text from html element that is a combobox. Every time I am getting blank text for the selected value in combobox.

Below is my html structure for combobox.

<ul id="servicetype-cave" class="z-combobox-content" style="height: auto;     width: auto;">
    <li id="zk_comp_140" class="z-comboitem">
    <li id="zk_comp_141" class="z-comboitem">
    <li id="zk_comp_142" class="z-comboitem">
    <li id="zk_comp_143" class="z-comboitem">
    <li id="zk_comp_144" class="z-comboitem z-comboitem-selected">
        <span class="z-comboitem-image"/>
        <span class="z-comboitem-text">Bill Grouping Service</span>
    </li>
    <li id="zk_comp_145" class="z-comboitem">
    <li id="zk_comp_146" class="z-comboitem">
    <li id="zk_comp_147" class="z-comboitem">
    <li id="zk_comp_148" class="z-comboitem">
    <li id="zk_comp_149" class="z-comboitem">
    <li id="zk_comp_150" class="z-comboitem">
</ul>

I have used below xpath for the element

@FindBy(xpath = "//ul[@id='servicetype-cave']/li[contains(@class,'z-comboitem-selected')]/span[@class='z-comboitem-text']")
public WebElement selectedServiceName;

public void selectService {
// Selecting an option from dropdown
WebElement tempEle = driver.findElement(By.xpath("(//div[@id='servicetype-pp']//span[2])[3]"));
commonFunctions.clickElement(tempEle);
String strtemp = selectedServiceName.getText();
System.out.println("Selected Service: " + strtemp);
}

Please help.

TIA.

2
  • Show code you use to get text Commented Aug 2, 2016 at 13:41
  • @Andersson: I have updated the question. Commented Aug 2, 2016 at 13:49

2 Answers 2

2

I'm suggesting you try using By.cssSelector() as below :-

WebElement el = driver.findElement(By.cssSelector("ul#servicetype-cave li.z-comboitem-selected span.z-comboitem-text"));

or

WebElement el = driver.findElement(By.cssSelector("ul#servicetype-cave li.z-comboitem.z-comboitem-selected span:nth-child(2)"));

Now use .getText() to getting the text as below :

el.getText();

If unfortunately .getText() does not work try using .getAttribute("textContent") as below :

el.getAttribute("textContent");

or try using .getAttribute("innerHTML") as below :

el.getAttribute("innerHTML");

Hope it works..:)

Sign up to request clarification or add additional context in comments.

3 Comments

Treid with getAttribute("textContent"). It worked perfectly fine.
@BhargavRaval it's may be designing issue that's why you are not getting by using .getText()...:)
Okkay. Cool. Thanks very much Buddy. Accepted... :)
0

@FindBy is eager internalization. That means it initializes when Page object creates. You should get value of combo-box after some preconditions, I assume. So make it use lazy initialization. Invoke following code just in place where you actually need it

findElement(By.xpath("//ul[@id='servicetype-cave']/li[contains(@class,'z-comboitem-selected')]/span[@class='z-comboitem-text']"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.