Select nested element by text using cssSelector - Selenium WebDriver
<p>I am trying to select an element based on the text. The problem I'm having is the element text exists multiple times in the source code. I was using xpath to find it (even though I try to avoid xpath), but that's all that I could get working.</p>

<p>Here is the html I'm working against:</p>

<pre><code>&lt;ul id="Dialogs_Dialogs_ctlAcknowledgeMessage_ConditionalDecline_cblMessage" class="cbl centered d2" style="width:511px;"&gt;
    &lt;li&gt;
        &lt;input id="Dialogs_Dialogs_ctlAcknowledgeMessage_ConditionalDecline_cblMessage_0" name="ctl00$ctl00$Dialogs$Dialogs$ctlAcknowledgeMessage_ConditionalDecline$cblMessage$0" onclick="CannedCommentSelected(this);" value="Complexity of job requires extra charge" type="checkbox"&gt;
        &lt;label for="Dialogs_Dialogs_ctlAcknowledgeMessage_ConditionalDecline_cblMessage_0"&gt;Complexity of job requires extra charge&lt;/label&gt;
    &lt;/li&gt;
    &lt;li&gt;
        &lt;input id="Dialogs_Dialogs_ctlAcknowledgeMessage_ConditionalDecline_cblMessage_1" name="ctl00$ctl00$Dialogs$Dialogs$ctlAcknowledgeMessage_ConditionalDecline$cblMessage$1" onclick="CannedCommentSelected(this);" value="Complexity of job requires extra time" type="checkbox"&gt;
        &lt;label for="Dialogs_Dialogs_ctlAcknowledgeMessage_ConditionalDecline_cblMessage_1"&gt;Complexity of job requires extra time&lt;/label&gt;
    &lt;/li&gt;
    &lt;li&gt;
        &lt;input id="Dialogs_Dialogs_ctlAcknowledgeMessage_ConditionalDecline_cblMessage_2" name="ctl00$ctl00$Dialogs$Dialogs$ctlAcknowledgeMessage_ConditionalDecline$cblMessage$2" onclick="CannedCommentSelected(this);" value="Current workload requires extra time" type="checkbox"&gt;
        &lt;label for="Dialogs_Dialogs_ctlAcknowledgeMessage_ConditionalDecline_cblMessage_2"&gt;Current workload requires extra time&lt;/label&gt;
    &lt;/li&gt;
    &lt;li&gt;
        &lt;input id="Dialogs_Dialogs_ctlAcknowledgeMessage_ConditionalDecline_cblMessage_3" name="ctl00$ctl00$Dialogs$Dialogs$ctlAcknowledgeMessage_ConditionalDecline$cblMessage$3" onclick="CannedCommentSelected(this);" value="Distance to property requires extra charge" type="checkbox"&gt;
        &lt;label for="Dialogs_Dialogs_ctlAcknowledgeMessage_ConditionalDecline_cblMessage_3"&gt;Distance to property requires extra charge&lt;/label&gt;
    &lt;/li&gt;
&lt;/ul&gt;
</code></pre>

<p>I was using <code>driver.findElement(By.xpath("//label[contains(text(), 'Complexity of job requires extra charge')]"));</code> but I started getting the "Element is not currently visible and so may not be interacted with" error which led me to discover there are multiple labels with the "Complexity of job requires extra charge" text.</p>

<p>I tried <code>driver.findElement(By.cssSelector("#Dialogs_Dialogs_ctlAcknowledgeMessage_ConditionalDecline_cblMessage &gt; #Dialogs_Dialogs_ctlAcknowledgeMessage_ConditionalDecline_cblMessage_0 &gt; label[text='Complexity of job requires extra charge']"));</code> but it timed out trying to find the element.</p>

<p>What am I doing wrong?</p>

<p>I even tried <code>driver.findElement(By.cssSelector("#Dialogs_Dialogs_ctlAcknowledgeMessage_ConditionalDecline_cblMessage &gt; input[value='Complexity of job requires extra charge']"));</code> which I was sure would work, but it times out looking for that also.</p>