2

I just got an error in Selenium(Java):

Unable to locate an element with the xpath expression //*[contains(.,'The field SomeField must be a string or array type with a maximum length of '60'.')]

Apparently, there are two ' which broke the expression. So I changed the code from

WebElement elem = findElement(By.xpath("//*[contains(.,'" + arg + "')]"));

to

WebElement elem = findElement(By.xpath("//*[contains(.,'" + arg.toString().replace("'", "\'") + "')]"));
WebElement elem = findElement(By.xpath("//*[contains(.,'" + arg.toString().replace("'", "\\'") + "')]"));
WebElement elem = findElement(By.xpath("//*[contains(.,'" + arg.toString().replace("'", "\\\'") + "')]"));

None of them worked. Now I temporarily work it out by doing this:

WebElement elem = findElement(By.xpath("//*[contains(.,\"" + arg + "\"')]"));

But the bug will come back if the arg contains " in it.

Anyone knows how to do that? Thanks for your help.

AndiCover
  • 1,724
  • 3
  • 17
  • 38
cheny
  • 2,545
  • 1
  • 24
  • 30
  • As the error shows... how long is `arg` if it's a long string you can reduce it as long as it's unique. – Moshe Slavin Dec 11 '19 at 15:13
  • It varies case by case. I tried to shorten it to remove ', but it could be dangerous since there might be similar text on the same page. So it's better that I try the whole string. – cheny Dec 11 '19 at 15:28

1 Answers1

3

Use String.format to build your xpath the following ways:

WebElement elem = findElement(By.xpath(String.format("//*[contains(.,\"%s\")]", arg)));

For further information about String.format take a look at it's documentation. The format arguments can be found here.


arg can only contain '

WebElement elem = findElement(By.xpath(String.format("//*[contains(.,\"%s\")]", arg)));

arg can only contain "

WebElement elem = findElement(By.xpath(String.format("//*[contains(.,'%s')]", arg)));

arg can contain both ' and "

Escape all " in arg with arg.replace("\"", """); and build your Xpath like

WebElement elem = findElement(By.xpath(String.format("//*[contains(.,\"%s\")]", arg)));
AndiCover
  • 1,724
  • 3
  • 17
  • 38
  • Thanks but it did not work for me. Now I'm using WebElement elem = findElement(By.xpath(String.format("//*[contains(.,'%s')]", arg))); and get error: Unable to locate an element with the xpath expression //*[contains(.,'The field SomeField must be a string or array type with a maximum length of '60'.')] because of .... Just the same as that of the very beginning. – cheny Dec 11 '19 at 14:30
  • @cheny I updated my answer. Can you try it again? Replaced `'` with `\"`. – AndiCover Dec 11 '19 at 14:39
  • Yes that works. But what if there is "(rather than ') in arg? – cheny Dec 11 '19 at 15:25
  • @cheny Updated my answer. – AndiCover Dec 11 '19 at 16:06
  • nice fix `arg.replace("\"", """)`!!! as per OP's question `String.format()` is the way to go! – Moshe Slavin Dec 12 '19 at 09:35
  • Hi guys I was busy on something else and just be back. Now I'm using WebElement elem = findElement(By.xpath(String.format("//*[contains(.,\"%s\")]", arg.toString().replace("\"", """)))); and get: Unable to locate element: {"method":"xpath","selector":"//*[contains(.,""name"")]"}. Did I missed something? – cheny Dec 12 '19 at 15:38
  • Can you share the HTML or URL? – AndiCover Dec 12 '19 at 16:55