0

how can I write xpath to identify 07971394283 in the following code :

<form id="HousingRepairs-confirmation-form" enctype="application/x-www-form-urlencoded" action="/communal-repairs-statuses-subscription/confirm.jsf?cid=1" method="post" name="HousingRepairs-confirmation-form"> 

    <input type="hidden" value="HousingRepairs-confirmation-form" name="HousingRepairs-confirmation-form"> <div class="fieldset-container"> 

    <p>Thank you for completing the communal repairs subscription form.</p> 

    <h3>What happens next?</h3> 
    <p> The mobile number **07971394283** will be added to the relevant subscription list. If you wish to unsubscribe at any time please use our <a id="HousingRepairs-confirmation-form:unSubscribeLink" href="https://contact.camden.gov.uk/unsubscribe-communal-repairs-alerts" name="HousingRepairs-confirmation-form:unSubscribeLink">communal repairs alerts unsubscription form</a>
  • What exactly do you want to find? Are you searching for `07971394283` in particular, trying to find any string of 11 digits, or trying to find any string of digits located in that position in the text? – AJMansfield Jan 07 '14 at 15:53
  • Also, you may want to try reading [ask]. Just following the few simple guidelines will make it far easier for you to get a good answer to your question. – AJMansfield Jan 07 '14 at 15:54
  • sorry for bad format..I am trying to find any string of 11 digits – user3169738 Jan 07 '14 at 15:56

3 Answers3

0

This should work:

"//p[contains(text(),'07971394283')]"

This XPath is searching for the first paragraph containing 07971394283.

EDIT: I just noticed that a string of digits is required rather than the specific number. In which case AJMansfield's response is the correct approach.

CynicalBiker
  • 579
  • 5
  • 14
0

In order to find a string of eleven digits, use a regular expression. From most basic to most complicated:

  • \d{11} will match any 11 digits in a row. This may not be desired, though, because it will also match the first 11 digits in a longer sequence. If there are longer strings of digits that need to be excluded, use a different solution.

  • \D(\d{11})\D will, as a whole, match any string composed of a non-digit character, eleven digits, and another non-digit character. The digits themselves will be matched as group 1. The problem with this approach is that it will be unable to match if the digits are at the very beginning or end of the string. If this is a possible case, there are other ways to handle it.

  • If your language supports lookahead/lookbehind, you can use (?<!\d)\d{11}(?!\d). This says, match a sequence of 11 digits that does not have another digit immediately before or after it. Unfortunately, lookahead/lookbehind is not always supported by all tools.

For information on how exactly to use this with xpath, there are a number of other resources on this topic:

Community
  • 1
  • 1
AJMansfield
  • 4,039
  • 3
  • 29
  • 50
  • Although this is right, I am not upvoting it purely on the fact that XPath v1 doesn't support regular expressions. His question was "what XPath & regular expression do I need to get this element*, you answered the second part but not the first - of which the answer is this is not possible in XPath & Selenium merely because none of the major browsers actually have XPath 2 engines - they are all using XPath 1. – Arran Jan 07 '14 at 18:28
0

It is not possible, merely because Selenium relies on the browser's XPath engine (and provides it's own subsistent in some cases), of which none of the major browsers actually support XPath 2 (required for regular expressions in XPath).

You will have to find the element first and then prove it has what you need inside it (the regular expressions posted elsewhere in this question will work).

Arran
  • 24,648
  • 6
  • 68
  • 78