Created
July 7, 2018 10:01
-
-
Save ego008/7c35e94fc9856e9619a6b110a42fbd5e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/ego008/selenium" | |
"net" | |
"time" | |
"os" | |
) | |
func pickUnusedPort() (int, error) { | |
addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0") | |
if err != nil { | |
return 0, err | |
} | |
l, err := net.ListenTCP("tcp", addr) | |
if err != nil { | |
return 0, err | |
} | |
port := l.Addr().(*net.TCPAddr).Port | |
if err := l.Close(); err != nil { | |
return 0, err | |
} | |
return port, nil | |
} | |
func main() { | |
port, err := pickUnusedPort() | |
fmt.Println("port", port) | |
opts := []selenium.ServiceOption{ | |
//selenium.StartFrameBuffer(), // Start an X frame buffer for the browser to run in. | |
//selenium.GeckoDriver(geckoDriverPath), // Specify the path to GeckoDriver in order to use Firefox. | |
selenium.Output(os.Stderr), // Output debug information to STDERR. | |
} | |
fmt.Println(opts) | |
selenium.SetDebug(false) | |
service, err := selenium.NewIeDriverService("IEDriverServer.exe", port, opts...) | |
if err != nil { | |
panic(err) // panic is used only as an example and is not otherwise recommended. | |
} | |
defer func() { | |
fmt.Println(service.Stop()) | |
}() | |
fmt.Println("here 1") | |
go func() { | |
//return | |
caps := selenium.Capabilities{"browserName": "internet explorer"} | |
wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d", port)) // http://localhost:%d/wd/hub | |
if err != nil { | |
panic(err) | |
} | |
defer func() { | |
wd.Quit() | |
}() | |
if err := wd.Get("https://www.facebook.com"); err != nil { | |
panic(err) | |
} | |
fmt.Println("-----3 ----") | |
fmt.Println(wd.WindowHandles()) | |
time.Sleep(time.Second*10) | |
fmt.Println(wd.ExecuteScript(`window.open("https://www.baidu.com", "_blank");`, nil)) | |
// webDriver.ExecuteScript(`window.open("`+href+`", "content-iframe");`, nil) | |
fmt.Println("-----4 ----") | |
fmt.Println(wd.WindowHandles()) | |
time.Sleep(time.Second*20) | |
fmt.Println("----go1 exit") | |
}() | |
// Connect to the WebDriver instance running locally. | |
caps := selenium.Capabilities{"browserName": "internet explorer"} | |
wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d", port)) // http://localhost:%d/wd/hub | |
if err != nil { | |
panic(err) | |
} | |
defer func() { | |
wd.Quit() | |
}() | |
// 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!\n") | |
} | |
`) | |
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) | |
} | |
fmt.Println("-----1 ----") | |
fmt.Println(wd.WindowHandles()) | |
var output string | |
for { | |
output, err = outputDiv.Text() | |
if err != nil { | |
panic(err) | |
} | |
if output != "Waiting for remote server..." { | |
break | |
} | |
time.Sleep(time.Second*1) | |
} | |
//fmt.Printf("%s", strings.Replace(output, "\n\n", "\n", -1)) | |
fmt.Println("waiting....") | |
time.Sleep(time.Second*80) | |
fmt.Println("-----2 ----") | |
fmt.Println(wd.WindowHandles()) | |
fmt.Println("----go2 exit") | |
// Example Output: | |
// Hello WebDriver! | |
// | |
// Program exited. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment