From 6b8830ca749af7078bec60f06217e25464ed9be3 Mon Sep 17 00:00:00 2001 From: Joseph Watson Date: Mon, 30 Aug 2021 12:31:14 -0400 Subject: [PATCH 1/2] Add ability to set the action for a form. This is something that is done in javascript on some sites. --- browser/browser.go | 1 - browser/form.go | 19 +++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/browser/browser.go b/browser/browser.go index 0632bdd..91d49e5 100644 --- a/browser/browser.go +++ b/browser/browser.go @@ -532,7 +532,6 @@ func (bow *Browser) SetHeadersJar(h http.Header) { bow.headers = h } -// SetTransport sets the http library transport mechanism for each request. // SetTimeout sets the timeout for requests. func (bow *Browser) SetTimeout(t time.Duration) { if bow.client == nil { diff --git a/browser/form.go b/browser/form.go index 87f10e6..ad8e23f 100644 --- a/browser/form.go +++ b/browser/form.go @@ -15,6 +15,10 @@ import ( type Submittable interface { Method() string Action() string + + // SetAction will change the forms Action to the provided url + SetAction(url string) error + Input(name, value string) error Set(name, value string) error @@ -113,6 +117,17 @@ func (f *Form) Action() string { return f.action } +// SetAction sets the form action URL. +// The provided URL may be relitive or absolute +func (f *Form) SetAction(url string) error { + action, err := f.bow.ResolveStringUrl(url) + if err != nil { + return err + } + f.action = action + return nil +} + // Input sets the value of a form field. // it returns an ElementNotFound error if the field does not exist func (f *Form) Input(name, value string) error { @@ -333,8 +348,8 @@ func (f *Form) send(buttonName, buttonValue string) error { if !ok { method = "GET" } - action, ok := f.selection.Attr("action") - if !ok { + action := f.action + if action == "" { action = f.bow.Url().String() } aurl, err := url.Parse(action) From 1beb27975ca8ffb804ce934c5774b0cb9252923c Mon Sep 17 00:00:00 2001 From: Joseph Watson Date: Fri, 12 Oct 2018 13:34:30 -0400 Subject: [PATCH 2/2] Add ability to add a button to a form. This is needed when emulating javascript. --- browser/form.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/browser/form.go b/browser/form.go index ad8e23f..038c188 100644 --- a/browser/form.go +++ b/browser/form.go @@ -69,6 +69,9 @@ type Submittable interface { // It will add the field to the form if necessary SetFile(name string, fileName string, data io.Reader) + // AddButton adds a new button to the collection of buttons in the form, with the given name and value. + AddButton(name, value string) + Click(button string) error ClickByValue(name, value string) error Submit() error @@ -308,6 +311,11 @@ func (f *Form) Submit() error { return f.send("", "") } +// AddButton adds a new button to the collection of buttons in the form, with the given name and value. +func (f *Form) AddButton(button, value string) { + f.buttons.Add(button, value) +} + // Click submits the form by clicking the button with the given name. func (f *Form) Click(button string) error { if _, ok := f.buttons[button]; !ok {