Search code examples
angularjsprotractorui-automationgettextgetattribute

Unable to fetch the value of an element in protractor


For one of my protractor script. I want to fetch the value of an element.

Here is the DOM setup:

<h5 class="ng-binding ng-scope" ng-if="editor.special.stock_number !== ''">
<b>Stock Number:</b>
 72850
</h5>

I want to fetch the value of Stock Number:

I tried using getText, but it just printed out the Stock Number:

this.get_stock_number = () => {
        let stockNumber = stock_number.getText();
        stockNumber.then((text) => {
            console.log("Stock Number is: " + text);
        });
    };

I also tried using getAttribute but it is returning null:

this.get_stock_number = () => {
        let stockNumber = stock_number.getAttribute('Stock Number:');
        stockNumber.then((text) => {
            console.log("Stock Number is: " + text);
        });
    };

I really need this thing to be sorted out. I am kind of stuck here. Any help will be much appreciated, Thanks.


Solution

  • You can try:

    • Locate the h5 element with the whole text and extract it

    h5element.getText()

    so it can return Ex:

    <b>Stock Number:</b> 72850

    • After that just extract the number from the string with regex
    let text = `<b>dsadsad</b> 320301`;
    let numberPattern = /\d+/g;
    let results = text.match(numberPattern);
    if (results != null) {
        let number = results[0];
        console.log(number);
    }