1

I'm using selenium/java and have a drop down list for languages. This list contains multiple variations of "English" as you can see in the code below. I want to select just "English". I thought of getting the div tag with span tag containing "English" and not containing "Creoles" (first option) and not containing open parenthesis.

Its omitting the first option but not reading the "(" which is after the span tag but still inside the div tag. How can I write the xpath to read the text inside div which is after the span element?

XPath:

//div[./span[text()='English'] and not(contains(text(),'Creoles')) and not(contains(text(), '('))]

HTML Code: (There are more variations of English but showing only 4 below)

<ul id="oj-listbox-results-contentItemId-67" class="oj-listbox-results" role="listbox">
<li class="oj-listbox-results-depth-0 oj-listbox-result oj-listbox-result-selectable" role="presentation">
    <div id="oj-listbox-result-label-6829" class="oj-listbox-result-label" role="option">
        Creoles and pidgins, 
        <span class="oj-listbox-highlighter">English</span>
        based
    </div>
</li>
<li class="oj-listbox-results-depth-0 oj-listbox-result oj-listbox-result-selectable" role="presentation">
    <div id="oj-listbox-result-label-6830" class="oj-listbox-result-label" role="option">
        <span class="oj-listbox-highlighter">English</span>
    </div>
</li>
<li class="oj-listbox-results-depth-0 oj-listbox-result oj-listbox-result-selectable" role="presentation">
    <div id="oj-listbox-result-label-6831" class="oj-listbox-result-label" role="option">
        <span class="oj-listbox-highlighter">English</span>
        (Australia)
    </div>
</li>
<li class="oj-listbox-results-depth-0 oj-listbox-result oj-listbox-result-selectable oj-hover" role="presentation">
    <div id="oj-listbox-result-label-6832" class="oj-listbox-result-label" role="option">
        <span class="oj-listbox-highlighter">English</span>
        (Belize)
    </div>
</li>
</ul>
3
  • Are the div id's consistent, and is the list sorted the same way every time? If so, you can select by index, or click the dropdown, then click the id on the selection you want.
    – Swagin9
    Feb 24, 2017 at 20:34
  • No, div ids are auto generated so not consistent.
    – Dougg
    Feb 24, 2017 at 21:03
  • Okay, are they displayed in the same order in the dropdown every time?
    – Swagin9
    Feb 24, 2017 at 21:31

2 Answers 2

1

On researching and reading further on this issue I saw few posts on using text() vs . I changed my xpath to use . instead of text() to read the text inside the div tag and it correctly identified those text. Since there is a space in the text for all other options except the one with just English, it works if I change my xpath to:

//div[./span[text()='English'] and not(contains(.,' '))]

It seems text() only reads the value until the next tag is encountered, in my case span tag but . (dot) can read values irrespective of other tags inside of the div tag.

This post has really good explanation: https://stackoverflow.com/questions/38240763/xpath-difference-between-dot-and-text

0

If the IDs on the DIVs are static (don't dynamically change each time you load the page) then you should be able click on it. You'll probably have to click on the dropdown first to display the options and then click on the individual English selection.

I don't write Java but in Ruby it would look something like:

@driver.find_element(id: 'oj-listbox-result-label-6831').click()

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.