[JAVA] How to find May'n in XPath

Recently, when I tried to find a character string containing'(single quote) in the environment of Selenium + Junit, which was taken care of by autopilot and autopilot test on the web page, I made a note.

If you want to search for a string that contains'(single quote) when identifying an element with Selenium etc., you have to escape'(single quote), but unfortunately the Xpath 1.0 specification There was no way to escape'(single quotes). From XPath 2.0, for example, to escape'(single quote) in a string enclosed in'(single quote), write two'(single quote) in a row and escape like''. However, it seems that XPath 2.0 cannot be used even with the latest version of Firefox and Chrome.

So what happened after all? It became as follows.

For example, when searching for an element containing the following character string by XPath,


<a aria-label="App May'n-chan&quot;Love&quot; <1>" href="#MarketListingPlace:p=tmp.03831173739257532153.1317485953465" data-column="TITLE"><img src="gwt/placeholder_icon_24.png " role="presentation"><div> <div>May'n-chan"Love" &lt;1&gt;</div> <div></div> </div> </a>

Do the following (when searching in the console of Firefox quantam)

$x("//a[@aria-label=concat('App May', \"'\", 'n-chan\"Love\" <1>')]")

With Selenium + Junit, it looks like the following

Search by By.xpath for Selenium + Junit


  searchString = "App May'n-chan\"Love\" <1>";
  xPathStringLiteral = toXPathStringLiteral(searchString);
  searchXpath = "//a[@aria-label=" + xPathStringLiteral + "]";



  private String toXPathStringLiteral(String string) {
    if ( string.indexOf("'") == -1 ) {
        return "'" + string + "'";
    if ( string.indexOf('"') == -1 ) {
        return '"' + string + '"';
    return ("concat('" + string.replace("'", "', \"'\", '") + "')");

It was terribly troublesome ...

Recommended Posts

How to find May'n in XPath
How to use Lombok in Spring
How to hide scrollbars in WebView
How to run JUnit in Eclipse
How to iterate infinitely in Ruby
[Rails] How to write in Japanese
How to find the average angle
How to run Ant in Gradle
How to master programming in 3 months
How to learn JAVA in 7 days
How to get parameters in Spark
How to install Bootstrap in Ruby
How to use InjectorHolder in OpenAM
How to introduce jQuery in Rails 6
How to use classes in Java?
How to name variables in Java
How to set Lombok in Eclipse
How to find Java prime numbers
How to concatenate strings in java
How to install Swiper in Rails
[swift5] How to specify color in hexadecimal
How to implement search functionality in Rails
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
How to change app name in rails
How to get date data in Ruby
How to use custom helpers in rails
How to reflect seeds.rb in production environment
How to use named volume in docker-compose.yml
How to filter JUnit Test in Gradle
How to insert a video in Rails
How to standardize header footer in Thymeleaf
How to include Spring Tool in Eclipse 4.6.3?
How to add jar file in ScalaIDE
How to do base conversion in Java
[Swift] How to fix Label in UIPickerView
How to have params in link_to method
How to use Docker in VSCode DevContainer
How to use MySQL in Rails tutorial
How to fix system date in JUnit
How to implement coding conventions in Java
How to embed Janus Graph in Java
[rails] How to configure routing in resources
How to map tsrange type in Hibernate
How to find the tens and ones
How to get the date in java
How to implement ranking functionality in Rails
How to use environment variables in RubyOnRails
How to implement asynchronous processing in Outsystems
How to publish a library in jCenter
How to specify id attribute in JSF
Understand in 5 minutes !! How to use Docker
How to overwrite Firebase data in Swift
How to use credentials.yml.enc introduced in Rails 5.2
How to assemble JSON directly in Jackson
How to execute multiple commands in docker-compose.yml
[For beginners] How to debug in Eclipse
How to use ExpandableListView in Android Studio
How to display error messages in Japanese
How to deploy