Using Chromedriver in Selenium - selenium

I tried installing neccessary deps with homebrew.
brew install selenium-server-standalone
brew install geckodriver
brew install chromedriver
But I can't get the selenium server to start. It hangs at the first panic.
panic: server did not respond on port 8080
import (
"fmt"
"github.com/tebeka/selenium"
"github.com/tebeka/selenium/chrome"
"os"
"strings"
"time"
)
func StartSession() {
// Start a Selenium WebDriver server instance (if one is not already running).
const (
// These paths will be different on your system.
seleniumPath = "/usr/local/Cellar/selenium-server-standalone/3.141.59_2/bin"
//geckoDriverPath = "/usr/local/Cellar/geckodriver/0.29.0"
chromeDriverPath = "/usr/local/bin/chromedriver"
port = 8080
)
opts := []selenium.ServiceOption{
//selenium.StartFrameBuffer(), // Start an X frame buffer for the browser to run in.
selenium.ChromeDriver(chromeDriverPath),
//selenium.GeckoDriver(geckoDriverPath), // Specify the path to GeckoDriver in order to use Firefox.
selenium.Output(os.Stderr), // Output debug information to STDERR.
}
selenium.SetDebug(true)
service, err := selenium.NewSeleniumService(seleniumPath, port, opts...)
if err != nil {
panic(err) // panic is used only as an example and is not otherwise recommended.
}
defer service.Stop()
// Connect to the WebDriver instance running locally.
//caps := selenium.Capabilities{"browserName": "firefox"}
caps := selenium.Capabilities{"browserName": "chrome"}
chromeCaps := chrome.Capabilities{
Path: "",
Args: []string{
"--headless", // <<<
"--no-sandbox",
"--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/604.4.7 (KHTML, like Gecko) Version/11.0.2 Safari/604.4.7",
},
}
caps.AddChrome(chromeCaps)
wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", port))
if err != nil {
panic(err)
}
defer wd.Quit()
}

seleniumPath = "/usr/local/Cellar/selenium-server-standalone/3.141.59_2/libexec/selenium-server-standalone-3.141.59.jar"
Setting to the path of the jar file works.

Related

How to load extension for Chrome in remote selenium using golang?

I have selenium in docker container(selenoid from aerocube) and selenium library for golang from tebeka.
I can't find any examples that show how to run chrome in a remote selenium with the extension (literally only for go)
I even found function in library which do it, but I did not found example of code where It was used.
(https://pkg.go.dev/github.com/tebeka/selenium#v0.9.9/chrome#Capabilities.AddExtension)
caps := selenium.Capabilities{"browserName": "chrome", "browserVersion": "103.0"}
driver, err := selenium.NewRemote(caps, "http://127.0.0.1:4444/wd/hub")
if err != nil {
fmt.Printf("create selenium session error: %v\n", err)
return
}
defer driver.Quit()
driver.Get("https://www.google.com/")
driver.Close()
I want to use the modheader extention, but I get the same question, and i solved it.
Step 1: Get your chrome extention (.crx) file
In my case, i find the modheader document,and get download link from the page.
https://docs.modheader.com/advanced/selenium-webdriver
Download the .crx file to you project.
the .crx download page
Download link
https://github.com/modheader/modheader_selenium/raw/main/chrome-modheader/modheader.crx
Note: Web browser maybe block the download by policy, use the "wget" command get the
file.
block by browser
wget the file
If you want to get other extention , use the follow CRX Extracti/Downloader can help you.
CRX Extracti link
CRX Extracti/Downloader webpage
Step 2: Use the Code loding the extention
package main
import (
"fmt"
"os"
"github.com/tebeka/selenium"
"github.com/tebeka/selenium/chrome"
)
const (
port = 8080
)
func main() {
opts := []selenium.ServiceOption{
// Enable fake XWindow session.
// selenium.StartFrameBuffer(),
selenium.Output(os.Stderr), // Output debug information to STDERR
}
_, err := selenium.NewChromeDriverService("../your_driver_path/chromedriver.exe", port, opts...)
if err != nil {
panic(err)
}
caps := selenium.Capabilities{"browserName": "chrome"}
var cap_ext chrome.Capabilities
// add your extention by crx file
cap_ext.AddExtension("./modheader.crx")
caps.AddChrome(cap_ext)
wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://127.0.0.1:%d/wd/hub", port))
// Using api to setting modheader
// add header
wd.Get("https://webdriver.modheader.com/add?test=ModHeader%20Test")
}
Step 3: Setting the extention
Modheader extention supply api to setting.
Example :
wd.Get("https://webdriver.modheader.com/add?test=ModHeader%20Test")
Step 4: Result
Loading and setting extention successful
I hope that I can help. Good Luck.

x509 certificate signed by unknown authority

I'm trying some basic examples to request data from the web, however all requests to different hosts result in an SSL error: x509: certificate signed by unknown authority. Note: I'm not behind a proxy and no forms of certificate interception is happening, as using curl or the browser works without problems.
The code sample I'm currently working with is:
package main
import (
"fmt"
"net/http"
"io/ioutil"
"os"
)
func main() {
response, err := http.Get("https://google.com")
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
} else {
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
fmt.Printf("%s\n", string(contents))
}
}
Edit: Code is run on Arch linux kernel 4.9.37-1-lts.
Edit 2: Apparently /etc/ssl/certs/ca-certificates.crt had a difference between the version on my system, by (re)moving the certificate and re-installing the ca-certificates-utils package manually, the issue was solved.
Based on your error, I'm assuming you are using Linux?
It's likely that you will have to install ca-certificates on the machine your program is running on.
On Ubuntu, you would execute something like this:
sudo apt-get install ca-certificates

Access HTTPS via HTTP proxy with basic authentication

I am trying to make a GET request against an HTTPS URL using proxy with username/password authorization (auth is required by the proxy not the website).
Here's what I do:
package main
import (
"crypto/tls"
"encoding/base64"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
)
func main() {
ua := "Mozilla/5.0 (Windows NT 6.1"
basic := "Basic " + base64.StdEncoding.EncodeToString([]byte("username:mypass"))
req, err := http.NewRequest("GET", "https://api.ipify.org/", nil)
proxyUrl, _ := url.Parse("http://myproxy.com:9999")
fmt.Println(basic) // Basic dXNlcm5hbWU6bXlwYXNz
req.Header.Add("Proxy-Authorization", basic)
req.Header.Add("User-Agent", ua)
bb, _ := httputil.DumpRequest(req, false)
fmt.Println(string(bb))
/*
Get / HTTP/1.1
Host: api.ipify.org
Proxy-Authorization: Basic dXNlcm5hbWU6bXlwYXNz
User-Agent: Mozilla/5.0 (Windows NT 6.1
*/
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Proxy: http.ProxyURL(proxyUrl),
},
}
resp, err := client.Do(req)
fmt.Println(err) // Proxy Authentication Required
fmt.Println(resp) // <nil>
}
The catch is that when I try to do a request to an HTTP (not HTTPS) site it goes fine, but if I make HTTPS request it fails (see above the message).
I tested the proxy with my browser (FireFox) and everything goes well, I looked for the headers through firebug and added everything relevant to the request (above). I've double-triple-checked the basic value and everything else but without any luck.
So does some one have any idea why this happens or at least how do I research the problem?
The last thing to add is that I can use a public HTTP proxy (that doesn't require any auth) in this case and problems seem to start when auth enters in this process (the error also suggests that).
P.S. Unfortunately I cannot share the proxy IP, port and username cause it is against their policy.
package main
import (
"crypto/tls"
"fmt"
"net/url"
"net/http"
)
func main() {
req, err := http.NewRequest("GET", "https://api.ipify.org/", nil)
proxyUrl, _ := url.Parse("http://username:password#127.0.0.1:9999")
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Proxy: http.ProxyURL(proxyUrl),
},
}
_, err = client.Do(req)
fmt.Println(err)
}

Go Lang exec/spawn a ssh session

I'm trying to work out the mechanism to fork/start a ssh terminal session
i.e I want to be logged into remote server (my keys are on server) if I execute this program.
Right now it just executes but nothing happens.
package main
import (
"os/exec"
"os"
)
func main() {
cmd := exec.Command("ssh","root#SERVER-IP")
cmd.Stdout = os.Stdout
//cmd.Stderr = os.Stderr
cmd.Run()
}
cmd.Run waits for the command to complete. Your ssh session should (normally) not exit without user interaction. Therefore your program blocks, since it waits for the ssh process to finish.
You may want to either
also redirect Stdin, so you can interact with the ssh session
execute ssh me#server somecommand. In the last form a specific command gets executed and the output of this command gets redirected.
take a look at the ssh package
I've done library that can cover your requiremenets: https://github.com/shagabutdinov/shell; checkout if it helps or not.
You can use this library to start ssh session and execute the commands:
key, err := ssh.ParsePrivateKey([]byte(YOUR_PRIVATE_KEY))
if(err != nil) {
panic(err)
}
shell, err = shell.NewRemote(shell.RemoteConfig{
Host: "root#example.com:22",
Auth: []ssh.AuthMethod{ssh.PublicKeys(key)},
})
if(err != nil) {
panic(err)
}
shell.Run("cat /etc/hostname", func(_ int, message string) {
log.Println(message) // example.com
})
This is simple wrapper over golang ssh library that helps to execute consequent commands over /bin/sh.

SCP Client in Go

I was struggling with and ssh client for golang but was told that the ciphers for the freesshd server was incompatible with the ssh client for Go, so I just installed another one (PowerShell Server) and I can successfully connect to the server.
My problem is not over because I now need to transfer files from local to remote, and this can only be done through scp. I was directed to this scp client for go and have two issues.
I run it and get this:
Where or how can I access the contents of id_rsa needed for privateKey? I just went in my .ssh folder and saw a github_rsa and used that private key, I'm sure that this is not the correct one to use but wouldn't I see some kind of error or invalid private key and not the result above?
The code you were directed to is broken. The example also uses the public key authentication, which is not necessarily your only option. If you can allow password authentication instead, you can make it a bit easier for yourself.
I just made an upload myself by modifying the example you used:
package main
import (
"code.google.com/p/go.crypto/ssh"
"fmt"
)
type password string
func (p password) Password(_ string) (string, error) {
return string(p), nil
}
func main() {
// Dial code is taken from the ssh package example
config := &ssh.ClientConfig{
User: "username",
Auth: []ssh.ClientAuth{
ssh.ClientAuthPassword(password("password")),
},
}
client, err := ssh.Dial("tcp", "127.0.0.1:22", config)
if err != nil {
panic("Failed to dial: " + err.Error())
}
session, err := client.NewSession()
if err != nil {
panic("Failed to create session: " + err.Error())
}
defer session.Close()
go func() {
w, _ := session.StdinPipe()
defer w.Close()
content := "123456789\n"
fmt.Fprintln(w, "C0644", len(content), "testfile")
fmt.Fprint(w, content)
fmt.Fprint(w, "\x00")
}()
if err := session.Run("/usr/bin/scp -qrt ./"); err != nil {
panic("Failed to run: " + err.Error())
}
}