using Selenium webdriver to find elements with &nb

2019-02-21 02:34发布

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

3条回答
放我归山
2楼-- · 2019-02-21 03:12

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

查看更多
够拽才男人
3楼-- · 2019-02-21 03:20
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 (i.e. unicode \u00A0) 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.

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-02-21 03:25

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));
查看更多
登录 后发表回答