我在Golang中有一个API,它try 连接到在docker映像中运行的Selify服务器:
package main
import (
"fmt"
"time"
"github.com/tebeka/selenium"
"github.com/tebeka/selenium/chrome"
)
func main() {
// Configuração webdriver (simulador)
caps := selenium.Capabilities{
"browserName": "chrome", "browserVersion": "114.0", "se:noVncPort": 7900, "se:vncEnabled": true,
}
chromeCaps := chrome.Capabilities{
Args: []string{
"--headless",
},
}
caps.AddChrome(chromeCaps)
// Iniciando o servidor, conectado ao eu webdriver que esta rodando
wd, err := selenium.NewRemote(caps, "http://localhost:4444/wd/hub")
if err != nil {
fmt.Printf("Falha ao iniciar o servidor Selenium: %s\n", err.Error())
return
}
defer wd.Quit()
time.Sleep(10 * time.Second)
// inicia a pagina inicial kk
err = wd.Get("https://www.csonline.com.br/")
if err != nil {
fmt.Printf("Falha ao abrir a página de login: %s\n", err.Error())
return
}
time.Sleep(10 * time.Second)
// Selecionar o elemento por ID
usernameField, err := wd.FindElement(selenium.ByID, "dfgd")
if err != nil {
fmt.Printf("Falha ao encontrar o campo de usuário: %s\n", err)
return
}
//populando os campos, ai é só repetir os passos
err = usernameField.SendKeys("dfglkdf)
if err != nil {
fmt.Printf("Falha ao preencher o campo de usuário: %s\n", err.Error())
}
passwordField, err := wd.FindElement(selenium.ByID, "dfg")
if err != nil {
fmt.Printf("Falha ao encontrar o campo de senha: %s\n", err.Error())
}
err = passwordField.SendKeys("dlfknvkxjcn ")
if err != nil {
fmt.Printf("Falha ao preencher o campo de senha: %s\n", err.Error())
}
// Envia o formulário de login
loginButton, err := wd.FindElement(selenium.ByCSSSelector, "#next")
if err != nil {
fmt.Printf("Falha ao encontrar o botão de login: %s\n", err.Error())
}
err = loginButton.Click()
if err != nil {
fmt.Printf("Falha ao clicar no botão de login: %s\n", err.Error())
}
fmt.Println("Sucess")
//aguarda um tempo para realizar login
time.Sleep(10 * time.Second)
Access, err := wd.FindElement(selenium.ByID, "btnSelectLogin")
if err != nil {
fmt.Printf("Falha ao encontrar o botão de acesso: %s\n", err)
}
err = Access.Click()
if err != nil {
fmt.Printf("Falha ao clicar no botão de acesso: %s\n", err)
}
fmt.Println("Sucess")
localStorageScript := `return localStorage.getItem("cs.token");`
// Execute o script no contexto do navegador
localStorageData, err := wd.ExecuteScript(localStorageScript, nil)
if err != nil {
fmt.Println("Falha ao obter dados do LocalStorage:", err)
}
// Imprima os dados do LocalStorage
fmt.Println("Bearer", localStorageData)
}
我在docker中安装了这样的selenium:
docker pull selenium/standalone-chrome
我运行以下命令以在docker中启动Selify:
docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome
运行命令时,请运行main.go 我在围棋终端中收到以下错误:
invalid session id: Unable to execute request for an existing session: Unable to find session with ID:
Build info: version: '4.10.0', revision: 'c14d967899'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.10.16.3-microsoft-standard-WSL2', java.version: '11.0.19'
Driver info: driver.version: unknown
在DOCKER中的selenium日志(log)中,我得到了:
WARN [SeleniumSpanExporter$1.lambda$export$3] - {"traceId": "171e16c9402c8f85639418c7b458f388","eventTime": 1687963769139610379,"eventName": "exception","attributes": {"exception.message": "Unable to execute request for an existing session: Unable to find session with ID: \nBuild info: version: '4.10.0', revision: 'c14d967899'\nSystem info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.10.16.3-microsoft-standard-WSL2', java.version: '11.0.19'\nDriver info: driver.version: unknown","exception.stacktrace": "org.openqa.selenium.NoSuchSessionException: Unable to find session with ID: \nBuild info: version: '4.10.0', revision: 'c14d967899'\nSystem info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.10.16.3-microsoft-standard-WSL2', java.version: '11.0.19'\nDriver info: driver.version: unknown\n\tat org.openqa.selenium.grid.sessionmap.local.LocalSessionMap.get(LocalSessionMap.java:137)\n\tat org.openqa.selenium.grid.router.HandleSession.lambda$loadSessionId$4(HandleSession.java:172)\n\tat io.opentelemetry.context.Context.lambda$wrap$2(Context.java:224)\n\tat org.openqa.selenium.grid.router.HandleSession.execute(HandleSession.java:125)\n\tat org.openqa.selenium.remote.http.Route$PredicatedRoute.handle(Route.java:384)\n\tat org.openqa.selenium.remote.http.Route.execute(Route.java:69)\n\tat org.openqa.selenium.remote.http.Route$CombinedRoute.handle(Route.java:347)\n\tat org.openqa.selenium.remote.http.Route.execute(Route.java:69)\n\tat org.openqa.selenium.grid.router.Router.execute(Router.java:87)\n\tat org.openqa.selenium.grid.web.EnsureSpecCompliantResponseHeaders.lambda$apply$0(EnsureSpecCompliantResponseHeaders.java:34)\n\tat org.openqa.selenium.remote.http.Filter$1.execute(Filter.java:63)\n\tat org.openqa.selenium.remote.http.Route$CombinedRoute.handle(Route.java:347)\n\tat org.openqa.selenium.remote.http.Route.execute(Route.java:69)\n\tat org.openqa.selenium.remote.http.Route$NestedRoute.handle(Route.java:271)\n\tat org.openqa.selenium.remote.http.Route.execute(Route.java:69)\n\tat org.openqa.selenium.remote.http.Route$CombinedRoute.handle(Route.java:347)\n\tat org.openqa.selenium.remote.http.Route.execute(Route.java:69)\n\tat org.openqa.selenium.remote.http.Route$CombinedRoute.handle(Route.java:347)\n\tat org.openqa.selenium.remote.http.Route.execute(Route.java:69)\n\tat org.openqa.selenium.remote.http.Route$CombinedRoute.handle(Route.java:347)\n\tat org.openqa.selenium.remote.http.Route.execute(Route.java:69)\n\tat org.openqa.selenium.remote.http.Route$CombinedRoute.handle(Route.java:347)\n\tat org.openqa.selenium.remote.http.Route.execute(Route.java:69)\n\tat org.openqa.selenium.remote.AddWebDriverSpecHeaders.lambda$apply$0(AddWebDriverSpecHeaders.java:35)\n\tat org.openqa.selenium.remote.ErrorFilter.lambda$apply$0(ErrorFilter.java:44)\n\tat org.openqa.selenium.remote.http.Filter$1.execute(Filter.java:63)\n\tat org.openqa.selenium.remote.ErrorFilter.lambda$apply$0(ErrorFilter.java:44)\n\tat org.openqa.selenium.remote.http.Filter$1.execute(Filter.java:63)\n\tat org.openqa.selenium.netty.server.SeleniumHandler.lambda$channelRead0$0(SeleniumHandler.java:44)\n\tat java.base\u002fjava.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)\n\tat java.base\u002fjava.util.concurrent.FutureTask.run(FutureTask.java:264)\n\tat java.base\u002fjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)\n\tat java.base\u002fjava.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)\n\tat java.base\u002fjava.lang.Thread.run(Thread.java:829)\n","exception.type": "org.openqa.selenium.NoSuchSessionException","http.flavor": 1,"http.handler_class": "org.openqa.selenium.grid.router.HandleSession","http.host": "localhost:4444","http.method": "POST","http.request_content_length": "38","http.scheme": "HTTP","http.target": "\u002fsession\u002f\u002furl","http.user_agent": "Go-http-client\u002f1.1","session.id": ""}}
这一切为什么要发生?有谁能帮上忙吗?