1

Below is the html and I want to get text "Conference USA"

<li class="list-unstyled" style="background-color: transparent;">
    <input id="account" type="checkbox" checked="checked" value="101" name="account" 
    style="background-color: rgb(255, 255, 255);"/> Conference USA                                 
</li>
4
  • <li class="list-unstyled" style="background-color: transparent;"> <input id="account" type="checkbox" checked="checked" value="101" name="account" style="background-color: rgb(255, 255, 255);"/> Conference USA <ul> </li> Commented Jul 7, 2016 at 14:16
  • Possible duplicate of Find element by Xpath which contains text Commented Jul 7, 2016 at 14:46
  • There are many questions like this one. Please do a search of SO or just google the question and you will find many answers that you can try. Commented Jul 7, 2016 at 14:46
  • Do you want to get text or locate input element by that text??? Commented Jul 8, 2016 at 2:51

3 Answers 3

3

According to your question and provided comments it's not clear what you want actually.

So if you want li element with using text Conference USA try as below :-

WebElement liElement = driver.FindElement(By.Xpath("//li[contains(.,'Conference USA')]"));

or if you want input element with using text Conference USA try as below :-

WebElement inputElement = driver.FindElement(By.Xpath("//li[contains(.,'Conference USA')]/input"));

or if you want text Conference USA of li element try as below :-

string text = driver.FindElement(By.Xpath("//li[./input[@type = 'checkbox' and @value='101']]")).Text;

Hope it will help you...:)

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

Comments

1

You should be able to uniquely locate li and getText() should work. For example in java it will look like:

String accountName = 
    driver.findElement(By.xpath(".//li[./input[@id='account']]")).getText();

EDIT c#:

driver.FindElement(By.Xpath(".//li[./input[@id='account']]")).Text;

4 Comments

Id= account is not unique in this case and I actually want to select li on the basis of text
what is your problem? do you want to get text or locate an element using known text? if you have unique and known value you can write .//li[./input[@value=value]]
i want to get text, but as you can see, that text is not inside "input" tag and that the reason I am not able to get that text. I am trying to locate that element "input" element using this text.
Thanks for your time though :)
0

Try getting "innerHTML" attribute from the li webelement like below, I had same situation earlier and it worked for me:

driver.findElement(By.xpath("//li[contains(@class,'list-unstyled')]")).getAttribute("innerHTML");

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.