英文:
Golang Docker Selenium Chrome
问题
这是一个使用Golang编写的API,尝试连接到在Docker镜像中运行的Selenium服务器。
你需要先安装Selenium的Docker镜像:
docker pull selenium/standalone-chrome
然后使用以下命令在Docker中启动Selenium:
docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome
当你运行go run main.go
命令时,在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日志中,你会看到以下错误信息:
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.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/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)\n\tat java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)\n\tat java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)\n\tat java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)\n\tat java.base/java.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": "/session//url","http.user_agent": "Go-http-client/1.1","session.id": ""}}
这个错误是由于无法找到会话ID导致的。可能是由于Selenium服务器无法与你的Golang代码建立连接。你可以检查以下几点:
- 确保Docker中的Selenium服务器正在运行,并且端口号为4444。
- 确保你的Golang代码中的远程地址与Docker中的Selenium服务器地址一致。
- 检查防火墙设置,确保允许Golang代码与Docker中的Selenium服务器进行通信。
如果问题仍然存在,你可以尝试使用其他方法连接到Selenium服务器,例如使用Selenium Grid或使用其他编程语言编写代码进行连接。
英文:
I have an api in golang that tries to connect to a selenium server that is running in a docker image:
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)
}
I installed selenium in docker like this:
docker pull selenium/standalone-chrome
i run this command to start selenium in docker:
docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome
when running the command go run main.go
i get this error in go terminal:
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
and in the selenium log that is in docker I get:
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": ""}}
why does this happen? does anyone have any help to offer?
答案1
得分: 1
要使您的演示与selenium/standalone-chrome:latest
(目前为selenium/standalone-chrome:4.10.0
)配合使用,您需要将W3C
设置为true
:
chromeCaps := chrome.Capabilities{
Args: []string{
"--headless",
},
W3C: true,
}
如果您仔细查看容器的日志,您应该会看到类似以下的内容:
> WARN [SeleniumSpanExporter$1.lambda$export$1] - org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Error while creating session with the driver service. Stopping driver service: Could not start a new session. Handshake response does not match any supported protocol. Response payload: {"sessionId":"4ef27eb4d2fbb1ebfe5cf2fb51df731b","status":33,"value":{"message":"session not created: Missing or invalid capabilities\n (Driver info: chromedriver=114.0.5735.90 (386bc09e8f4f2e025eddae123f36f6263096ae49-refs/branch-heads/5735@{#1052}),platform=Linux 5.19.0-43-generic x86_64)"}}
这可能是由于Selenium 4.9.0中引入的破坏性更改引起的。
我从您的演示中删除了"browserVersion": "114.0"
,然后对selenium/standalone-chrome:4.8.3
和selenium/standalone-chrome:4.9.0
进行了测试。结果是它与4.8.3
一起工作,但与4.9.0
不起作用。这至少证明了在4.9.0
中引入了一个破坏性更改。
英文:
To make your demo work with selenium/standalone-chrome:latest
(which is selenium/standalone-chrome:4.10.0
as of now), you need to set W3C
to true
:
chromeCaps := chrome.Capabilities{
Args: []string{
"--headless",
},
W3C: true,
}
If you look at the log of the container carefully, you should see something like this:
> WARN [SeleniumSpanExporter$1.lambda$export$1] - org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Error while creating session with the driver service. Stopping driver service: Could not start a new session. Handshake response does not match any supported protocol. Response payload: {"sessionId":"4ef27eb4d2fbb1ebfe5cf2fb51df731b","status":33,"value":{"message":"session not created: Missing or invalid capabilities\n (Driver info: chromedriver=114.0.5735.90 (386bc09e8f4f2e025eddae123f36f6263096ae49-refs/branch-heads/5735@{#1052}),platform=Linux 5.19.0-43-generic x86_64)"}}
This could be caused by the breaking change introduced by Remove Json Wire Protocol support, which was shipped in Selenium 4.9.0
.
I removed "browserVersion": "114.0"
from your demo, then tested it against selenium/standalone-chrome:4.8.3
and selenium/standalone-chrome:4.9.0
. The result is that it works with 4.8.3
but doesn't with 4.9.0
. That at least proves that a breaking change is introduced to 4.9.0
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论