| 1 |
// Package main ... |
| 2 |
package main |
| 3 |
|
| 4 |
import ( |
| 5 |
"fmt" |
| 6 |
"log" |
| 7 |
"net" |
| 8 |
"net/http" |
| 9 |
|
| 10 |
"github.com/go-rod/rod" |
| 11 |
"github.com/go-rod/rod/lib/input" |
| 12 |
) |
| 13 |
|
| 14 |
// This example demonstrates how to send key events to an element. |
| 15 |
func main() { |
| 16 |
page := rod.New().MustConnect().MustPage(testServer()) |
| 17 |
|
| 18 |
val1 := page.MustElement("#input1").MustText() |
| 19 |
// cSpell:ignore naoeu |
| 20 |
val2 := page.MustElement("#textarea1").MustInput("\b\b\n\naoeu\n\ntest1\n\nblah2\n\n\t\t\t\b\bother box!\t\ntest4").MustText() |
| 21 |
val3 := page.MustElement("#input2").MustInput("test3").MustText() |
| 22 |
val4 := page.MustElement("#select1").MustType(input.ArrowDown, input.ArrowDown).MustProperty("value").Str() |
| 23 |
|
| 24 |
log.Printf("#input1 value: %s", val1) |
| 25 |
log.Printf("#textarea1 value: %s", val2) |
| 26 |
log.Printf("#input2 value: %s", val3) |
| 27 |
log.Printf("#select1 value: %s", val4) |
| 28 |
} |
| 29 |
|
| 30 |
// testServer is a simple HTTP server that displays elements and inputs. |
| 31 |
func testServer() string { |
| 32 |
l, _ := net.Listen("tcp4", "127.0.0.1:0") |
| 33 |
go func() { |
| 34 |
_ = http.Serve(l, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 35 |
_, _ = fmt.Fprint(w, indexHTML) |
| 36 |
})) |
| 37 |
}() |
| 38 |
return "http://" + l.Addr().String() |
| 39 |
} |
| 40 |
|
| 41 |
const indexHTML = `<!doctype html> |
| 42 |
<html> |
| 43 |
<head> |
| 44 |
<title>example</title> |
| 45 |
</head> |
| 46 |
<body> |
| 47 |
<div id="box1" style="display:none"> |
| 48 |
<div id="box2"> |
| 49 |
<p>box2</p> |
| 50 |
</div> |
| 51 |
</div> |
| 52 |
<div id="box3"> |
| 53 |
<h2>box3</h3> |
| 54 |
<p id="box4"> |
| 55 |
box4 text |
| 56 |
<input id="input1" value="some value"><br><br> |
| 57 |
<textarea id="textarea1" style="width:500px;height:400px">textarea</textarea><br><br> |
| 58 |
<input id="input2" type="submit" value="Next"> |
| 59 |
<select id="select1"> |
| 60 |
<option value="one">1</option> |
| 61 |
<option value="two">2</option> |
| 62 |
<option value="three">3</option> |
| 63 |
<option value="four">4</option> |
| 64 |
</select> |
| 65 |
</p> |
| 66 |
</div> |
| 67 |
</body> |
| 68 |
</html>` |