-
Notifications
You must be signed in to change notification settings - Fork 21
/
remote_driver_element.go
87 lines (70 loc) · 1.79 KB
/
remote_driver_element.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package goselenium
import (
"encoding/json"
"errors"
"fmt"
)
type findElementResponse struct {
E element `json:"value"`
}
type findElementsResponse struct {
E []element `json:"value"`
}
type element struct {
ID string `json:"element"`
}
func (s *seleniumWebDriver) FindElement(by By) (Element, error) {
if by.Type() == "index" {
return nil, errors.New("findelement: invalid by argument")
}
if len(s.sessionID) == 0 {
return nil, newSessionIDError("FindElement")
}
var response findElementResponse
var err error
url := fmt.Sprintf("%s/session/%s/element", s.seleniumURL, s.sessionID)
resp, err := s.elementRequest(&elRequest{
url: url,
by: by,
method: "POST",
callingMethod: "FindElement",
})
if err != nil {
return nil, err
}
err = json.Unmarshal(resp, &response)
if err != nil {
return nil, newUnmarshallingError(err, "FindElement", string(resp))
}
el := newSeleniumElement(response.E.ID, s)
return el, nil
}
func (s *seleniumWebDriver) FindElements(by By) ([]Element, error) {
if by.Type() == "index" {
return nil, errors.New("findelements: invalid by argument")
}
if len(s.sessionID) == 0 {
return nil, newSessionIDError("FindElements")
}
var response findElementsResponse
var err error
url := fmt.Sprintf("%s/session/%s/elements", s.seleniumURL, s.sessionID)
resp, err := s.elementRequest(&elRequest{
url: url,
by: by,
method: "POST",
callingMethod: "FindElements",
})
if err != nil {
return nil, err
}
err = json.Unmarshal(resp, &response)
if err != nil {
return nil, newUnmarshallingError(err, "FindElements", string(resp))
}
elements := make([]Element, len(response.E))
for i := range response.E {
elements[i] = newSeleniumElement(response.E[i].ID, s)
}
return elements, nil
}