简体   繁体   中英

using Selenium webdriver to find elements with  

Hope you're all well. I'm quite new to selenium and a complete newbie to stack overflowso i hope i'm not disobeying any rules here.

I am trying to create a test framework and some tests using the following

c#, selenium webdriver, specflow, nunit

I am currently running a query against a sql server DB to retrieve some text and then trying to use that text to verify and search for that element on the site. The problem i have is that the site has the element html encoded

For example:

Data retrieved from the database could be something like - "Alumina MB China metallurgical grade delivered...."

The element on the site is as follows -

 <span class="com">Alumina&nbsp;MB&nbsp;China&nbsp;metallurgical&nbsp;grade&nbsp;delivered&nbsp;duty&nbsp;paid&nbsp;RBM/tonne</span> 

Does anyone know how i could parse the data retrieved from the DB into the findElement command to easily find this element on the site whilst it contains &nbsp;

Look forward to hearing from you.

Many Thanks Suban

Try to escape html characters by using System.Web.HttpUtility.HtmlDecode

it works for me:

var escaped = System.Web.HttpUtility.HtmlDecode(".//span[text()='Alumina&nbsp;MB&nbsp;China&nbsp;metallurgical&nbsp;grade&nbsp;delivered&nbsp;duty&nbsp;paid&nbsp;RBM/tonne']");
IWebElement element = driver.FindElement(By.XPath(escaped));

you should first replace all instances of $nbsp; in your xpath string to and then search for that

IWebElement element = driver.FindElement(By.XPath("//span[contains(@class'com') and normalize-space(translate(., '\u00A0', ' '))='Alumina MB China metallurgical grade delivered']"));

Explanation: Use xpath to find a span element with class containing the string com , get the text from that ( . ) replace all of the &nbsp characters (ie unicode ) with a regular space character, and strip leading and trailing whitespace (this last part may not be needed in your situation).

Note: the string that should go in the single quotes after the = should only contain regular spaces.

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