I have the following element in the DOM:
<div id="sourceListDiv">
<select id="sourceList" onkeydown="uil.handleKeyDown(this)" size="15" ondblclick="uil.HandleDBLClickOnSourceList(this);">
<option id="{88175AC5-E53B-4E67-826C-0E2A43A4E910}">Approval Process Type - (Approval Process Type)</option>
<option id="{3CFB4862-B80B-45AD-B21E-82E377A59CD5}">Days Before Next Billing - (Calculates Days before Next Installment Due Date)</option>
<option id="{800F55B6-1603-4FD5-ADCD-D030C2EFBAC9}">Days In Collection - (Calculates Days in Collection)</option>
<option id="{5531AA05-A347-472C-8B5B-2CFAFE5542E6}">Days Past Due - (Delay days coming from Host)</option>
<option id="{D7D18409-9685-4EEA-B664-2570288566D0}">Days in Strategy - (Days in Strategy)</option>
</select>
</div>
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.
1 Answer 1
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.
Explore related questions
See similar questions with these tags.