简体   繁体   English

java selenium xpath以查找包含空格的文本元素

[英]java selenium xpath to find element with text containing whitespaces

I have an element that I want to find by using universal xpath searching for defined text. 我有一个要通过使用通用xpath搜索定义的文本来查找的元素。 Problem is that in one of elements text is separated by lots of whitespaces. 问题在于,在元素之一中,文本被许多空白分隔。

<div class="form-group">
    <label class="col-sm-4 control-label" 
    for="demo-hor-inputemail">AP Document
                                            Type *</label>
    <div class="form-group">
        <div class="col-sm-8"> 
            <div>
                <ul>
                    <li class="active-result>option</li>
                </ul>
             </div>
         </div>
    </div>
<div>

My current working code is something like this: 我当前的工作代码是这样的:

public static void dropdown(String dropdown, String option) {
    drop_down = "//*/label[contains(text(), '" + dropdown + "')]";
    driver.findElement(By.xpath(drop_down + "/following-sibling::div[1]").click;
    driver.findElement(By.xpath(drop_down + 
        "/following-sibling::div/descendant::li[starts-with(text(),'"+ option +"')]")).click;
}

I need to find this element by passing text "AP Document Type" as dropdown parameter. 我需要通过传递文本“ AP Document Type”作为下拉参数来找到此元素。

I cannot use just "AP Document" because of having element with that same text. 我不能仅使用“ AP文档”,因为元素具有相同的文本。 I have tried passing text with all white spaces but that did not work. 我尝试传递带有所有空格的文本,但这没有用。 I have tried a few modifiers like this: [normalize-space(text()) = '"+dropdown+"'] but without luck. 我尝试了一些这样的修饰符: [normalize-space(text()) = '"+dropdown+"']但没有运气。

Only way that I have found so far is by passing "Type *" as dropdown variable but I'm seeking for better solution because this case would not work if there were no * in text. 到目前为止,我发现的唯一方法是将“ Type *”作为下拉变量传递,但我正在寻求更好的解决方案,因为如果文本中没有*,这种情况将不起作用。

Thanks to Florent B. and Andersson, I found a solution. 感谢Florent B.和Andersson,我找到了一个解决方案。

"//label[contains(normalize-space(), '" + dropdown + "')]" works great. "//label[contains(normalize-space(), '" + dropdown + "')]"效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM