forked from sourcegraph/go-selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
49 lines (41 loc) · 1.16 KB
/
example_test.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
package selenium_test
import (
"fmt"
"sourcegraph.com/sourcegraph/go-selenium"
)
func ExampleFindElement() {
var webDriver selenium.WebDriver
var err error
caps := selenium.Capabilities(map[string]interface{}{"browserName": "firefox"})
if webDriver, err = selenium.NewRemote(caps, "http://localhost:4444/wd/hub"); err != nil {
fmt.Printf("Failed to open session: %s\n", err)
return
}
defer webDriver.Quit()
err = webDriver.Get("https://github.com/sourcegraph/go-selenium")
if err != nil {
fmt.Printf("Failed to load page: %s\n", err)
return
}
if title, err := webDriver.Title(); err == nil {
fmt.Printf("Page title: %s\n", title)
} else {
fmt.Printf("Failed to get page title: %s", err)
return
}
var elem selenium.WebElement
elem, err = webDriver.FindElement(selenium.ByCSSSelector, ".author")
if err != nil {
fmt.Printf("Failed to find element: %s\n", err)
return
}
if text, err := elem.Text(); err == nil {
fmt.Printf("Author: %s\n", text)
} else {
fmt.Printf("Failed to get text of element: %s\n", err)
return
}
// output:
// Page title: GitHub - sourcegraph/go-selenium: Selenium WebDriver client for Go
// Author: sourcegraph
}