/
postblog.go
140 lines (123 loc) · 3.33 KB
/
postblog.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package oneday
import (
"fmt"
"os"
"strings"
"time"
"github.com/tebeka/selenium"
)
// https://github.com/tebeka/selenium/blob/master/example_test.go
// https://juejin.cn/post/6949557048203804709
const (
// 设置 ChromeDriver 的路径
chromeDriverPath = "D:\\doc\\2023\\05-third\\chromedriver_win32\\chromedriver.exe"
port = 8080
)
func PostGeekTime() {
//Start a ChromeDriver server instance
opts := []selenium.ServiceOption{
selenium.Output(os.Stderr), // Output debug information to STDERR.
}
selenium.SetDebug(true)
service, err := selenium.NewSeleniumService(chromeDriverPath, port, opts...)
if err != nil {
panic(err) // panic is used only as an example and is not otherwise recommended.
}
defer service.Stop()
// // 设置 Chrome 的选项
// // Connect to the WebDriver instance running locally.
// chromeCaps := chrome.Capabilities{
// Path: "",
// Args: []string{
// "--headless",
// "--disable-gpu",
// "--no-sandbox",
// "--disable-dev-shm-usage",
// "--disable-extensions",
// "--disable-infobars",
// "--disable-features=NetworkService",
// },
// }
chromeCaps := selenium.Capabilities{"browserName": "chrome"}
// 连接到 Chrome 浏览器
wd, err := selenium.NewRemote(chromeCaps, fmt.Sprintf("http://localhost:%d/wd/hub", port))
if err != nil {
fmt.Println(err)
return
}
defer wd.Quit()
// 打开要发帖的网站
// if err := wd.Get("https://example.com"); err != nil {
// fmt.Println(err)
// return
// }
// // 等待页面加载完成
// time.Sleep(5 * time.Second)
// // 填写表单并提交
// if err := wd.FindElement(selenium.ByCSSSelector, "#title").SendKeys("这是标题"); err != nil {
// fmt.Println(err)
// return
// }
// if err := wd.FindElement(selenium.ByCSSSelector, "#content").SendKeys("这是内容"); err != nil {
// fmt.Println(err)
// return
// }
// if err := wd.FindElement(selenium.ByCSSSelector, "#submit").Click(); err != nil {
// fmt.Println(err)
// return
// }
// // 等待页面加载完成
// time.Sleep(5 * time.Second)
// // 打印发帖结果
// fmt.Println("发帖成功!")
// Navigate to the simple playground interface.
// Navigate to the simple playground interface.
if err := wd.Get("http://play.golang.org/?simple=1"); err != nil {
panic(err)
}
// Get a reference to the text box containing code.
elem, err := wd.FindElement(selenium.ByCSSSelector, "#code")
if err != nil {
panic(err)
}
// Remove the boilerplate code already in the text box.
if err := elem.Clear(); err != nil {
panic(err)
}
// Enter some new code in text box.
err = elem.SendKeys(`
package main
import "fmt"
func main() {
fmt.Println("Hello WebDriver!")
}
`)
if err != nil {
panic(err)
}
// Click the run button.
btn, err := wd.FindElement(selenium.ByCSSSelector, "#run")
if err != nil {
panic(err)
}
if err := btn.Click(); err != nil {
panic(err)
}
// Wait for the program to finish running and get the output.
outputDiv, err := wd.FindElement(selenium.ByCSSSelector, "#output")
if err != nil {
panic(err)
}
var output string
for {
output, err = outputDiv.Text()
if err != nil {
panic(err)
}
if output != "Waiting for remote server..." {
break
}
time.Sleep(time.Millisecond * 100)
}
fmt.Printf("%s", strings.Replace(output, "\n\n", "\n", -1))
}