Rv Reddy
Rv Reddy

Reputation: 3

extracting part of success text using selenium webdriver

I have the following text appearing on the success page of my application.

This is to confirm that your application has been received. Your Order Number is “#00007942”. If further instructions or any clarification is needed regarding your application, a representative will contact you.

Complete text having same property.

Please help me in extracting the value 00007942 and store it in variable.

Upvotes: 0

Views: 1668

Answers (2)

Naveen Kumar R B
Naveen Kumar R B

Reputation: 6398

there is no way to retrieve partial text in selenium webdriver.

Instead, you access the complete text of an Web Element using element.getText() in Java or element.text in python and store it as a String variable.

Then you process the string to retrieve the substring you want.

In all programming languages, there are many ways to achieve it. some of them are substring method, regular expression.

Upvotes: 2

optimistic_creeper
optimistic_creeper

Reputation: 2799

First, get your text in your way.

String successMessage = driver.findElement(By.cssSelector("your selector")).getText(); // use locator of your wish

Now, use replace all non-digit from your string as follows-

String orderNumber = successMessage.replaceAll("\\D+", ""); // this replaces all non-digits from your previous string

Upvotes: 2

Related Questions