2

I have this set of html:

<div class="container-fluid">
<div class="panel panel-default ">
    <div class="panel-body">
        <form id="coupon_checkout" action="http://uat.all.com.my/checkout/couponcode" method="post">
            <input type="hidden" name="transaction_id" value="4245">
            <input type="hidden" name="lang" value="EN">
            <input type="hidden" name="devicetype" value="">
            <div class="input-group">
                <input type="text" class="form-control" id="coupon_code" name="coupon" placeholder="Coupon Code">
                <span class="input-group-btn">
                <button class="btn btn-primary" type="submit">Enter Code</button>
                </span>
            </div>
        </form>
    </div>
</div>

I want to get the transaction id value. How do i get it? I have try using this code:

var value = wkWebView.evaluateJavaScript("document.getElementByName('transaction_id')", completionHandler: nil)

print("value:\(value)")

But the output is return nothing:

value:( )

4 Answers 4

1

Two things - you need an "s" in the getElementsByName code and it returns a collection of elements - you need to specify which - in this case there is only 1, but you still need to specify it and get the value of the element:

...document.getElementsByName('transaction_id')[0].value....
Sign up to request clarification or add additional context in comments.

1 Comment

I don't know swift at all - but normally i would expect you to get the value - it seems to me as if you are getting the element but not the value of the lements - try ...document.getElementsByName('transaction_id')[0].value ....
1

I think it's a typo, you should use getElementsByName instead of getElementByName.

Second thing: Shouldn't it be "document.getElementsByName('transaction_id').value" rather than "document.getElementByName('transaction_id')"

You can also use it like if heaving issues:

webView.evaluateJavaScript("document.getElementsByName('someElement').value") { (result, error) in
        if error != nil {
            print(result)
        }
    }

2 Comments

Ha - Ha - @Pravesh Agrawal - already posted that - its a race with these answers :)
ha ha - @Pravesh Agrawal - this time you got there ahead of me - i posted my answer re - the .value and then saw your edited answer saying the same thing :) - but you still need the [0] in there to specify which element in the collection to reference
0

I have just popped it into this snippet and provided the value in a jquery approach (since you are using Bootstrap - you must also be using jQuery) and the normal javascript method - both work so it must be something to do with the swift content. - also in your output - are you sure that the "\" is not escaping the "("? - should it be print("value:(value)") or escape both print("value:\(value\)") or alter the quotes print("value:"(value))

$(document).ready(function(){
  var transaction_idValue1 = $('[name=transaction_id]').val();
  console.log(transaction_idValue1);
  
 var transaction_idValue2 = document.getElementsByName('transaction_id')[0].value;
  console.log(transaction_idValue2);
  
  })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container-fluid">
                                <div class="panel panel-default ">
            <div class="panel-body">
                <form id="coupon_checkout" action="http://uat.all.com.my/checkout/couponcode" method="post">
                    <input type="hidden" name="transaction_id" value="4245">
                    <input type="hidden" name="lang" value="EN">
                    <input type="hidden" name="devicetype" value="">
                    <div class="input-group">
                        <input type="text" class="form-control" id="coupon_code" name="coupon" placeholder="Coupon Code">
                        <span class="input-group-btn">
                            <button class="btn btn-primary" type="submit">Enter Code</button>
                        </span>
                    </div>
                </form>
            </div>
        </div>

Comments

0

Using SwiftSoup

let html = "<div class=\"container-fluid\">"
    + "<div class=\"panel panel-default \">"
    + "<div class=\"panel-body\">"
    + "<form id=\"coupon_checkout\" action=\"http://uat.all.com.my/checkout/couponcode\" method=\"post\">"
    + "<input type=\"hidden\" name=\"transaction_id\" value=\"4245\">"
    + "<input type=\"hidden\" name=\"lang\" value=\"EN\">"
    + "<input type=\"hidden\" name=\"devicetype\" value=\"\">"
    + "<div class=\"input-group\">"
    + "<input type=\"text\" class=\"form-control\" id=\"coupon_code\" name=\"coupon\" placeholder=\"Coupon Code\">"
    + "<span class=\"input-group-btn\">"
    + "<button class=\"btn btn-primary\" type=\"submit\">Enter Code</button>"
    + "</span>"
    + "</div>"
    + "</form>"
    + "</div>"
    + "</div>"
let document: Document = try SwiftSoup.parse(html);//parse html
let elements = try document.select("[name=transaction_id]")//query
let transaction_id = try elements.get(0)//select first element , 
let value = try transaction_id.val()//get value
print(value)//4245

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.