我正在尝试根据文本选择元素。我遇到的问题是元素文本在源代码中多次存在。我正在使用xpath来找到它(尽管我试图避免使用xpath),但这就是我能够工作的全部内容。
以下是我反对的HTML:
<ul id="Dialogs_Dialogs_ctlAcknowledgeMessage_ConditionalDecline_cblMessage" class="cbl centered d2" style="width:511px;">
<li>
<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">
<label for="Dialogs_Dialogs_ctlAcknowledgeMessage_ConditionalDecline_cblMessage_0">Complexity of job requires extra charge</label>
</li>
<li>
<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">
<label for="Dialogs_Dialogs_ctlAcknowledgeMessage_ConditionalDecline_cblMessage_1">Complexity of job requires extra time</label>
</li>
<li>
<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">
<label for="Dialogs_Dialogs_ctlAcknowledgeMessage_ConditionalDecline_cblMessage_2">Current workload requires extra time</label>
</li>
<li>
<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">
<label for="Dialogs_Dialogs_ctlAcknowledgeMessage_ConditionalDecline_cblMessage_3">Distance to property requires extra charge</label>
</li>
</ul>
我正在使用driver.findElement(By.xpath("//label[contains(text(), 'Complexity of job requires extra charge')]"));
,但我开始使用&#34;元素目前不可见,因此可能无法与&#34;错误导致我发现有多个标签与&#34;复杂的工作需要额外的费用&#34;文本。
我尝试了driver.findElement(By.cssSelector("#Dialogs_Dialogs_ctlAcknowledgeMessage_ConditionalDecline_cblMessage > #Dialogs_Dialogs_ctlAcknowledgeMessage_ConditionalDecline_cblMessage_0 > label[text='Complexity of job requires extra charge']"));
,但是它试图找到该元素的时间超时。
我做错了什么?
我甚至尝试了driver.findElement(By.cssSelector("#Dialogs_Dialogs_ctlAcknowledgeMessage_ConditionalDecline_cblMessage > input[value='Complexity of job requires extra charge']"));
,我确信它会起作用,但它也会超时寻找。
答案 0 :(得分:1)
在这种情况下,您应该尝试使用JavascriptExecutor
与此进行互动,如下所示: -
WebElement el = driver.findElement(By.cssSelector("input[value='Complexity of job requires extra charge']"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();",el);
已修改: -
List<WebElement> els = driver.findElements(By.cssSelector("input[value='Complexity of job requires extra charge']"));
for(WebElement el : els)
{
if(el.isDisplayed() && el.isEnabled())
{
el.click();
}
}
希望它有帮助...:)
答案 1 :(得分:1)
cssSelector
:
>
用于直接子项,但ID为cblMessage
和cblMessage_0
的元素之间有另一个元素,而id为{{1}的元素你正在寻找的元素是兄弟姐妹。
您可以使用此cblMessage_0
找到包含文本
cssSelector
driver.findElement(By.cssSelector("#Dialogs_Dialogs_ctlAcknowledgeMessage_ConditionalDecline_cblMessage_0 ~ label"));
代表兄弟姐妹。
答案 2 :(得分:0)
你的CSS没有用,因为它试图找到标签作为输入的孩子,这实际上是一个兄弟姐妹。您可以告诉xpath也可以通过ID查找父级ul。以下XPath应该可以工作:
//ul[@id="Dialogs_Dialogs_ctlAcknowledgeMessage_ConditionalDecline_cblMessage"]/li/label[contains(text(), 'Complexity of job requires extra charge')]
如果您真的有兴趣选择输入以便检查该框,请尝试以下操作:
//ul[@id="Dialogs_Dialogs_ctlAcknowledgeMessage_ConditionalDecline_cblMessage"]/li/label[contains(text(), 'Complexity of job requires extra charge')]/preceding-sibling::input