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.
Related
I'm trying to automate a test where I need to upload a file, but I keep getting that the file can't be found.
I found in Selenium page that, for other languages, we have to use a Local File Detector so that the file that's in my computer are sent to the remote server.
The Local File Detector allows the transfer of files from the client machine to the remote server.
But I can't find any function related to that in Go.
I've tried the SendKeys function:
element, err := crediya.el.FindElement(selenium.ByID, "file")
if err != nil {
return fmt.Errorf(errors.Selenium.ElementNotFound+"\n%w", err)
}
if err = element.SendKeys(path); err != nil {
return fmt.Errorf(errors.Selenium.SendKeysFailure+"\n%w", err)
}
but I keep getting:
"unknown error - 61: invalid argument: File not found : "
Note: the element is an input of file type.
Thank you, in advance, for any help provided.
Can someone, please, help me?
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.
I'm starting to learn golang and I'm trying to make a simple http client that will get a list of virtual machines from one of our oVirt clusters. The API that I'm trying to access has a self-signed certificate (auto generated during the cluster installation) and golang's http.client encounters a problem when serializing the time from the certificate. Below you can find the code and the output.
package main
import (
"fmt"
"io/ioutil"
"net/http"
"crypto/tls"
)
func do_request(url string) ([]byte, error) {
// ignore self signed certificates
transCfg := &http.Transport{
TLSClientConfig: &tls.Config {
InsecureSkipVerify: true,
},
}
// http client
client := &http.Client{Transport: transCfg}
// request with basic auth
req, _ := http.NewRequest("GET", url, nil)
req.SetBasicAuth("user","pass")
resp, err := client.Do(req)
// error?
if err != nil {
fmt.Printf("Error : %s", err)
return nil, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
return []byte(body), nil
}
func main() {
body, _ := do_request("https://ovirt.example.com/")
fmt.Println("response Status:", string(body))
}
and the error when I'm trying to compile:
$ go run http-get.go
Error : Get https://ovirt.example.com/: tls: failed to parse certificate from server: asn1: time did not serialize back to the original value and may be invalid: given "141020123326+0000", but serialized as "141020123326Z"response Status:
Is there any way to ignore this verification? I tried making a request using other programming languages (python, ruby) and skipping insecure certificates seems to be enough.
Thank you!
PS: I know the proper solution is to change the certificate with a valid one, but for the moment I cannot do this.
Unfortunately, you've encountered an error that you cannot get around in Go. This is buried deep in the cypto/x509 and encoding/asn1 packages without a way to ignore. Specifically, asn1.parseUTCTime is expecting the time format to be "0601021504Z0700", but your server is sending "0601021504+0000". Technically, that is a known format but encoding/asn1 does not support it.
There are only 2 solutions that I can come up with that do not require a code change for golang.
1) Edit the encoding/asn1 package in your go src directory and then rebuild all the standard packages with go build -a
2) Create your own customer tls, x509 and asn1 packages to use the format your server is sending.
Hope this helps.
P.S. I've opened an issue with the Go developers to see if it can resolved by them at some later point Issue Link
Possible ASN1 UtcTime Formats.
I have a script that works in PhantomJS but I'm trying to switch to SlimerJS. I keep getting an error when trying to open a local file:
var webPage = require('webpage');
var system = require('system');
var page = webPage.create();
page.viewportSize = { width: 2048, height: 1536 };
console.log('Processing',system.args[1]);
page.open(
'simple.html',
function start(status) {
setTimeout(function(){
page.render(system.args[2], {format: 'png'});
phantom.exit();
},1000);
}
);
simple.html is a file located in the same directory as the script. The resulting PNG says "Address Not Found", "simple.html could not be found. Please check the name and try again."
I've also tried:
full OS path, eg /User/blah/blah/simple.html
file URI file:///Users/blah/blah/simple.html
These yield a similar result.
I'd rather not have the script publicly available for a variety of reasons. Is it possible to launch a local file with SlimerJS?
I don't think its possible. Reading the docs it specifies a url.
I got around this by running a http server
python -m SimpleHTTPServer
Then accessing it through localhost.
page.open('http://localhost:8000/simple.html',...)
A file URI does work. Something like file:///Users/name/project/file.html.
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.