简体   繁体   中英

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. 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.

I cannot use just "AP Document" because of having element with that same text. 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.

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.

Thanks to Florent B. and Andersson, I found a solution.

"//label[contains(normalize-space(), '" + dropdown + "')]" works great.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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