Problem finding element with text when the text contains &nbsb;



  • I have the following element in the DOM:

    And need to use xpath in order to locate one of the options.

    The code I am executing is this:

                searchForLookUpResult(itemName);
                clickOnLookUpSearchButton();
                System.out.println(driver.findElement(By.xpath("//div[@id='sourceListDiv']/select/option")).getText());
                itemName = itemName.replace(" ", "${nbsp}");
                System.out.println(itemName);
                driver.findElement(By.xpath("//option[text()=\"" + itemName + "\"]")).click();
                driver.findElement(By.xpath("ui_okButton")).click();
                if (frameContext.equals("Internal")) { switchToFrameManually(1); }
                else switchToDialogFrame();
    

    But, it fails when it reaches the: driver.findElement(By.xpath("//option[text()=\"" + itemName + "\"]")).click(); part with the error:

    org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//option[text()="Days${nbsp}Past${nbsp}Due${nbsp}-${nbsp}(Delay${nbsp}days${nbsp}coming${nbsp}from${nbsp}Host)"]"}

    I took the ${nbsp} part from selenium in order to handle the   in the DOM (saw it here: https://stackoverflow.com/questions/247135/using-xpath-to-search-text-containing-nbsp )

    The itemName parameter should be passed as a String because I am using it in a previous part of the same method. The value of the String passed is: "Days Past Due - (Delay days coming from Host)".

    So far, all efforts have failed, with or without replace(), typing the ${nbsp} and or   or its \u00A0 replacement. So any hints will be highly appreciated.



  • After quite some search I found the solution on a c# related topic (I use Java, so I missed that one).

    Modified my code to this:

            searchForLookUpResult(itemName);
            clickOnLookUpSearchButton();
            driver.findElement(By.xpath("//option[normalize-space(translate(., '\u00A0', ' '))='" + itemName + "']")).click();
            clickDialogOk();
            if (frameContext.equals("Internal")) { switchToFrameManually(1); }
            else switchToDialogFrame();
    

    Apparently the normalize-space in coordination with the translate, did the trick and the element was uniquely identified.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2