[WebDriver] cannot verify text present if the content contains special characters

5,355 views
Skip to first unread message

shen xieyin

unread,
Jul 31, 2012, 10:42:13 AM7/31/12
to webdriver
Hi all,

We implemented the a legacy RC API isTextPresent with the pure webdrive API as we are moving our Selenium1 to Selenium2 (no WebdriveBackedSelenim as well), the logic is like:


 this.isElementPresent(new Locator("//*[contains(.,'" + text + "')]"), timeout); 

Note: isElementPresent is our own API to check if an element exits in a page, and text is the pass-in text we want to check. If works well with the regular text, but if we give some special character like quote, it gave me the following error.

passed in text: test, test' "test'

org.openqa.selenium.InvalidSelectorException: The given selector //*[contains(.,'test, test' "test'')] is either invalid or does not result in a WebElement. The following error occurred:
[InvalidSelectorError] Unable to locate an element with the xpath expression //*[contains(.,'test, test' "test'')] because of the following error:
[Exception... "The expression is not a legal expression."  code: "51" nsresult: "0x805b0033 (NS_ERROR_DOM_INVALID_EXPRESSION_ERR)"  location: "file:///C:/Users/jshen/AppData/Local/Temp/anonymous4442777811371808560webdriver-profile/extensions/fxdr...@googlecode.com/components/driver_component.js Line: 2438"]
Command duration or timeout: 100 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '2.21.0', revision: '16552', time: '2012-04-11 19:08:38'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_33'
Driver info: driver.version: RemoteWebDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:175)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:128)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:459)
at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:246)
at org.openqa.selenium.remote.RemoteWebDriver.findElementsByXPath(RemoteWebDriver.java:316)
at org.openqa.selenium.By$ByXPath.findElements(By.java:339)
at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:215)

anyone can help take a look?


selenium version. 2.21.


Thanks,
-Shen

Mark Collin

unread,
Jul 31, 2012, 11:04:03 AM7/31/12
to webd...@googlegroups.com

Java question really.

 

You will need to show us the code for isElementPresent and the value of text that you are passing in.

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.

shen xieyin

unread,
Jul 31, 2012, 11:08:32 AM7/31/12
to webd...@googlegroups.com
Mark, 

The text is  test, test' "test' that I have mentioned already.


findElementWrapper is the wrapper we do to use findElement with different By condition, and it works fine with the different type of locator and the regular text, but now our problem is the text contains the special character like quote, see above. thanks for quick reply.

long timer = 0l;
   
    System.out.println("Searching for Locator object value <"+locatorObj.getLocatorKey()+"> - START");
   
    if (timeout == 0)
    {       
       this.findElementWrapper( locatorObj);
       
       return ( getWebElement() == null ) ? false : getWebElement().isDisplayed();
    }
   
    // Check if the polling time value is greater than the timeout value.
    // If it's so the polling value gets the value of a timeout.
    pollingTime = ( pollingTime > timeout) ? timeout : pollingTime;
   
    while (true)
    {    
    if ( timer < timeout )
    {    
    this.findElementWrapper( locatorObj);
   
    if ( getWebElement() == null )
        {
        try
        {        
        // Sleep for 5 seconds.
    Thread.sleep( pollingTime * 1000 );
   
    timer += pollingTime;
   
        catch (InterruptedException e){}
        }
    else
    {    
    System.out.println("Searching for Locator object value <"+locatorObj.getLocatorKey()+"> - END");
   
    // Element is found!
    return this.getWebElement().isDisplayed();
    }
    }
    else
    { // Timed-out!
    return false;
    }    

2012/7/31 Mark Collin <mark....@lazeryattack.com>

Mark Collin

unread,
Jul 31, 2012, 11:37:34 AM7/31/12
to webd...@googlegroups.com

That just gives me more questions.

 

Is the the code bloc of isElementPresent?  (I’m expecting to see the declaration of the function as well as the code)

 

What is:

 

·         The code for findElementWrapper()

·         The code for getWebElement()

·         locatorObj (it looks like and object)

·         pollingTime (it doesn’t seem to be defined)

 

I would guess that at some point you need to do something along the lines of:

 

locator = locator.replaceAll("'", "\\'");

 

It’s a real shot in the dark at the moment though.

shen xieyin

unread,
Jul 31, 2012, 6:12:10 PM7/31/12
to webd...@googlegroups.com
locator = locator.replaceAll("'", "\\'");  still does not, and I got the same answer. 

to answer the question.

  private int findElementWrapper(Locator locatorObj)
    {
        By byInstance = null;
        try
        {
            byInstance = locatorObj.populateByClass();
            String locatorValue = locatorObj.getLocatorValue();
            
            if (locatorValue != null)
            {
                System.out.println("Locator value is: " + locatorValue);
            }
        }
     
        catch (Exception e) 
        { // In case something goes wrong when we try to figure out By class.
        throw new RuntimeException("Unable to determine <By> class type for Locator object <"+locatorObj.getLocatorKey()+"> - " + e);
        }
        
    List <WebElement> collection = this.getWebDriver().findElements(byInstance);
       
    // It's OK to have webElement object being set to a "null" since this method is being called continuously in IsElementPresent() method
    // until a predefined timeout value is up.
    if (! collection.isEmpty() )
    {
    this.webElement = collection.get(0);
    }
   
    return collection.size();
    }

getWebElement -  just a getter to retrieve the webelement object.

locatorObj  - it's our wrapper, you can image it's the by conduction, such as xpath, name, id, we pass in to find a element.

pollingTime  -   private long                 pollingTime                 = 5l;


2012/7/31 Mark Collin <mark....@lazeryattack.com>

Mark Collin

unread,
Aug 1, 2012, 1:25:08 AM8/1/12
to webd...@googlegroups.com

I’m pretty sure it’s going wrong inside locatorObj, which is where you should be doing the replaceAll.

 

Can you show the code for locatorObj.

Mark Collin

unread,
Aug 1, 2012, 11:39:40 AM8/1/12
to webd...@googlegroups.com

Have you tried with \\” ?  in java you need to escape the \

 

From: webd...@googlegroups.com [mailto:webd...@googlegroups.com] On Behalf Of Adrian Marton
Sent: 01 August 2012 09:44
To: webd...@googlegroups.com
Subject: [webdriver] Re: [WebDriver] cannot verify text present if the content contains special characters

 

Hello,

    I have the same problem.If you try  to  find an element by xpath and the text contains quotes then it will fail.I've tried to replace the \" with his ascii code, or special characte like &#32; but it's not working.
      
          text= "a\"b";
          test.findElement(By.xpath(text));// will fail
     
    This is a normal behavior because you can do something like this:

          test.findElement(By.xpath("./option[@text()=\" + value + "\"]"))  and it will work fine if the value variable doesn't contain any special characters, but if for example value contains \" then By.xpath will not
          interpret the expression correctly.

--

You received this message because you are subscribed to the Google Groups "webdriver" group.

To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/CiqVbjJ_4RsJ.

shen xieyin

unread,
Aug 1, 2012, 9:36:00 PM8/1/12
to webd...@googlegroups.com
Mark,

I have any question that if we use this logic to implement original RC API isTextPresent ( "//*[contains(.,'" + text + "')]" ), it will fetch content in JS code and HMTL comments as well, but you know what we need is only search all the literal text in the page. Do you have suggestion for this point?


Thanks,
-Shen

2012/8/1 Mark Collin <mark....@lazeryattack.com>

David

unread,
Aug 2, 2012, 2:46:22 AM8/2/12
to webd...@googlegroups.com
Then wouldn't the following XPath work for narrow down to page content only? I don't think you can filter out HTML comments unless you can do an XPath exclusion in the expression, otherwise manually filter the resulting text with regular expressions.

"//body[contains(.,'"+text+"')]"

Another option is to not use XPath but to query for the body text (innerText DOM attribute of body tag) then regular expression match against it.

String result = driver.findElement(By.tagName("body")).getAttribute("innerText"); //then regex this result for text to find

I think innerText will strip out HTML comments. If you use innerHTML instead, that would include comments inside body tag.

2012/8/1 Mark Collin <mark....@lazeryattack.com>

[Exception... "The expression is not a legal expression."  code: "51" nsresult: "0x805b0033 (NS_ERROR_DOM_INVALID_EXPRESSION_ERR)"  location: "file:///C:/Users/jshen/AppData/Local/Temp/anonymous4442777811371808560webdriver-profile/extensions/fxdri...@googlecode.com/components/driver_component.js Line: 2438"]

Command duration or timeout: 100 milliseconds

For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html

Build info: version: '2.21.0', revision: '16552', time: '2012-04-11 19:08:38'

System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_33'

Driver info: driver.version: RemoteWebDriver

            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

            at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)

            at java.lang.reflect.Constructor.newInstance(Unknown Source)

            at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:175)

            at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:128)

            at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:459)

            at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:246)

            at org.openqa.selenium.remote.RemoteWebDriver.findElementsByXPath(RemoteWebDriver.java:316)

            at org.openqa.selenium.By$ByXPath.findElements(By.java:339)

            at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:215)

 

anyone can help take a look?

 

 

selenium version. 2.21.

 

 

Thanks,

-Shen

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/CiqVbjJ_4RsJ.
To post to this group, send email to webd...@googlegroups.com.

To unsubscribe from this group, send email to webdriver+unsubscribe@googlegroups.com.


For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+unsubscribe@googlegroups.com.

Adrian Marton

unread,
Aug 5, 2012, 6:25:38 AM8/5/12
to webd...@googlegroups.com
Hello,

   I have tried this solution too, but it's not working.  I don't know why, but in any other method if you are using text = "a\"b" and you are passing it to a method , let's say parse(String text) , it will work just fine, but
the By.xpath will no take parameter a\"b  but the direct parsed value a"b. So something is wrong in the By.xpath implementation.

Smita Sinha

unread,
Aug 6, 2012, 4:20:05 AM8/6/12
to webd...@googlegroups.com
Its working for me ..check this out

HTML:
<div>
<span>a"b</span>
</div>

Code:
  WebElement b = driver.findElement(By.xpath("//span[text()='a\"b']"));
 System.out.println(b.getText().toString());

O/P:
a"b

-Smita

Adrian Marton

unread,
Aug 6, 2012, 4:26:28 AM8/6/12
to webd...@googlegroups.com
Hmm, that's strange. Can you tell me which version of selenium are using?

Smita Sinha

unread,
Aug 6, 2012, 4:30:22 AM8/6/12
to webd...@googlegroups.com
2.23.1 tried it with 2.19 also.Its working!

-Smita

Adrian Marton

unread,
Aug 6, 2012, 4:52:59 AM8/6/12
to webd...@googlegroups.com
Hello ,
  
  Thank you! Mine i think is 2.15 ,i am using an Testing tool , and for now they integrated only this version. I hope it will work in the next release.

Mark Collin

unread,
Aug 6, 2012, 5:13:19 AM8/6/12
to webd...@googlegroups.com

The latest version is 2.25.0, you are very out of date.

 

Have a look at the fixes and additions since 2.15.0 here:

 

http://selenium.googlecode.com/svn/trunk/java/CHANGELOG

Adrian Marton

unread,
Aug 6, 2012, 5:52:56 AM8/6/12
to webd...@googlegroups.com
I noticed that I'm using a very old version. The problem is that I'm using Twist which integrates a lot of other libraries and they can't make every time a release when selenium is updated.I will have to wait.

Thank you Mark for the link.

Mark Collin

unread,
Aug 6, 2012, 6:53:34 AM8/6/12
to webd...@googlegroups.com
http://www.thoughtworks-studios.com/twist/1.0/help/how_do_i_use_other_selenium_versions_with_twist.html#


-----Original Message-----
From: webd...@googlegroups.com [mailto:webd...@googlegroups.com] On Behalf Of Adrian Marton
Sent: 06 August 2012 10:53
To: webd...@googlegroups.com
Subject: Re: [webdriver] Re: [WebDriver] cannot verify text present if the content contains special characters

I noticed that I'm using a very old version. The problem is that I'm using Twist which integrates a lot of other libraries and they can't make every time a release when selenium is updated.I will have to wait.

Thank you Mark for the link.

On Mon, Aug 6, 2012 at 12:13 PM, Mark Collin
<mark....@lazeryattack.com>wrote:

> The latest version is 2.25.0, you are very out of date.****
>
> ** **
>
> Have a look at the fixes and additions since 2.15.0 here:****
>
> ** **
>
> http://selenium.googlecode.com/svn/trunk/java/CHANGELOG****
>
> ** **
>
> ** **
>
> *From:* webd...@googlegroups.com [mailto:webd...@googlegroups.com]
> *On Behalf Of *Adrian Marton
> *Sent:* 06 August 2012 09:53
> *To:* webd...@googlegroups.com
> *Subject:* Re: [webdriver] Re: [WebDriver] cannot verify text present
> if the content contains special characters****
>
> ** **
>
> Hello ,
>
> Thank you! Mine i think is 2.15 ,i am using an Testing tool , and
> for now they integrated only this version. I hope it will work in the
> next
> release.****
>
> On Mon, Aug 6, 2012 at 11:30 AM, Smita Sinha <sayhi....@gmail.com>
> wrote:****
>
> 2.23.1 tried it with 2.19 also.Its working!****
>
> ** **
>
> -Smita****
>
> ** **
>
> On Mon, Aug 6, 2012 at 1:56 PM, Adrian Marton <marto...@gmail.com>
> wrote:****
>
> Hmm, that's strange. Can you tell me which version of selenium are
> using?*
> ***
>
> ** **
>
> On Mon, Aug 6, 2012 at 11:20 AM, Smita Sinha <sayhi....@gmail.com>
> wrote:****
>
> Its working for me ..check this out****
>
> ** **
>
> HTML:****
>
> <div>****
>
> <span>a"b</span>****
>
> </div>****
>
> ** **
>
> Code:****
>
> WebElement b =
> driver.findElement(By.xpath("//span[text()='a\"b']"));***
> *
>
> System.out.println(b.getText().toString());****
>
> ** **
>
> O/P:****
>
> a"b****
>
> ** **
>
> -Smita****
>
> ** **
>
> On Sun, Aug 5, 2012 at 3:55 PM, Adrian Marton <marto...@gmail.com>
> wrote:****
>
> Hello,
>
> I have tried this solution too, but it's not working. I don't know
> why, but in any other method if you are using text = "a\"b" and you
> are passing it to a method , let's say parse(String text) , it will
> work just fine, but the By.xpath will no take parameter a\"b but the
> direct parsed value a"b=

Adrian Marton

unread,
Aug 6, 2012, 7:56:11 AM8/6/12
to webd...@googlegroups.com
Hello Mark,

  Thank you very much for the link, it is very usefull.

Reply all
Reply to author
Forward
0 new messages