Coder Social home page Coder Social logo

Comments (32)

stevepryde avatar stevepryde commented on July 21, 2024 1

Ok so I managed to get something working.

In order to use --connect-existing you have to start firefox with the -marionette option (I assume you know this already).
Then I started geckodriver using --connect-existing --marionette-port=2828 (2828 is the default marionette port).

Then I was able to run the tokio_async example (I changed it to use firefox). Haven't tried window handles yet

from thirtyfour.

stevepryde avatar stevepryde commented on July 21, 2024 1

There is also a quit_on_drop flag on driver that is currently not public. I will make that public so that people can choose to leave the browser session open if they wish. Unfortunately when I do that, geckodriver thinks its session is also still running so it won't start another :(

It should be possible to reconnect to an existing session if you know the id though. However I don't know if thirtyfour supports this nicely yet. That's a separate issue though.

from thirtyfour.

MTRNord avatar MTRNord commented on July 21, 2024 1

Bugzilla issue is now at https://bugzilla.mozilla.org/show_bug.cgi?id=1682062

from thirtyfour.

stevepryde avatar stevepryde commented on July 21, 2024 1

Closing. Please open a new issue if this is not fixed in Firefox by now.

from thirtyfour.

stevepryde avatar stevepryde commented on July 21, 2024

Hmm, I suspect the issue may simply be that geckodriver cannot start a new browser session. I'm not sure if connecting to an existing browser is possible.

Geckodriver can only support one single session at a time. If you want to run multiple sessions of Firefox, you will need to use selenium.

from thirtyfour.

MTRNord avatar MTRNord commented on July 21, 2024

Ah hm. In that case I wonder how I can find a specific tab? Thats what I wanted to do with that but I am guessing I am completely wrong with this.

from thirtyfour.

stevepryde avatar stevepryde commented on July 21, 2024

Again I am not sure if that is possible. WebDriver/selenium typically wants to start its own session and own it. I don't think you can start your own browser session and then connect to it with geckodriver. Webdriver wants to create a session (which starts a new browser instance) and return a session id and it uses that session id to control the browser.

If you start the browser yourself you won't have a session id so my understanding is that it won't work.

Still, geckodriver should be able to start a new session despite an existing browser running. Make sure it's the right version of geckodriver for the version of Firefox you have installed. Once geckodriver has a session running though, it cannot start another until the first one ends

from thirtyfour.

stevepryde avatar stevepryde commented on July 21, 2024

Have you tried doing the same thing with another programming language? Maybe I'm misunderstanding what you are trying to do. If it's possible in python with selenium, for example, then it should be possible with thirtyfour (and vice versa)

from thirtyfour.

MTRNord avatar MTRNord commented on July 21, 2024

geckodriver has a --connect-existing option. I can get the currently selected tab (for example title command works). It can do one session but certainly complains if you start a second session. So the connecting to the browser seems to work but not the GetWindowHandles command it seems

from thirtyfour.

MTRNord avatar MTRNord commented on July 21, 2024

Have you tried doing the same thing with another programming language? Maybe I'm misunderstanding what you are trying to do. If it's possible in python with selenium, for example, then it should be possible with thirtyfour (and vice versa)

No I did not.

So from the beginning:

My goal is to automate my daily use firefox. (Parts of what i do binding to macros). I am able to connect to that window via geckodriver just fine (title command and current_window_handle work). What is not working is getting all window handles and therefor switching tabs.

I am not using selenium in between geckodriver and thirtyfour.

from thirtyfour.

stevepryde avatar stevepryde commented on July 21, 2024

Hmm ok. I didn't know about that option. I will have a look into it.

from thirtyfour.

MTRNord avatar MTRNord commented on July 21, 2024

Thanks :) The flag used is this: https://firefox-source-docs.mozilla.org/testing/geckodriver/Flags.html#code-connect-existing-code

from thirtyfour.

stevepryde avatar stevepryde commented on July 21, 2024

It just occurred to me that the browser session will close on drop, and I can't remember if it's possible to disable that currently

from thirtyfour.

MTRNord avatar MTRNord commented on July 21, 2024

I did work around that using a once_cell "singleton" :) This leaves it always as a global static variable. Certainly not the correct way but it does what I want it too and does not close it. To allow another session I just have to restart geckodriver.

async fn get_firefox_driver() -> Result<&'static GenericWebDriver<ReqwestDriverAsync>> {
    use once_cell::sync::OnceCell;
    static INSTANCE: OnceCell<GenericWebDriver<ReqwestDriverAsync>> = OnceCell::new();
    if let Some(driver) = INSTANCE.get() {
        Ok(driver.clone())
    } else {
        let caps = DesiredCapabilities::firefox();
        let driver = WebDriver::new("http://localhost:4444/", &caps).await?;

        // TODO handle errors
        INSTANCE.set(driver);
        Ok(INSTANCE.get().unwrap().clone())
    }
}

from thirtyfour.

stevepryde avatar stevepryde commented on July 21, 2024

I opened two tabs in firefox and then ran the following:
This works for me:

    let handles = driver.window_handles().await?;
    println!("Window handles: {:?}", handles);
    driver.switch_to().window(&handles[0]).await?;

    // Navigate to https://wikipedia.org.
    driver.get("https://wikipedia.org").await?;

    driver.switch_to().window(&handles[1]).await?;
    driver.get("https://google.com").await?;
    driver.switch_to().window(&handles[0]).await?;

I get the following output:

Window handles: [WindowHandle { handle: "17" }, WindowHandle { handle: "38" }]

It opened wikipedia in the first tab and google in the second

from thirtyfour.

MTRNord avatar MTRNord commented on July 21, 2024

I opened two tabs in firefox and then ran the following:
This works for me:

    let handles = driver.window_handles().await?;
    println!("Window handles: {:?}", handles);
    driver.switch_to().window(&handles[0]).await?;

    // Navigate to https://wikipedia.org.
    driver.get("https://wikipedia.org").await?;

    driver.switch_to().window(&handles[1]).await?;
    driver.get("https://google.com").await?;
    driver.switch_to().window(&handles[0]).await?;

I get the following output:

Window handles: [WindowHandle { handle: "17" }, WindowHandle { handle: "38" }]

It opened wikipedia in the first tab and google in the second

Hm weird I still get the error. Running firefox 83.0 on linux and geckodriver 0.26.0. Doing exactly the same things as you did.

from thirtyfour.

MTRNord avatar MTRNord commented on July 21, 2024

Note: I have uhm like 9ish windows open with multiple tabs. So maybe it gets confused on firefox side due to that many windows?

from thirtyfour.

stevepryde avatar stevepryde commented on July 21, 2024

I'm using geckodriver 0.28.0 - try updating yours and see if that works

from thirtyfour.

MTRNord avatar MTRNord commented on July 21, 2024

I'm using geckodriver 0.28.0 - try updating yours and see if that works

ah yeah I can do that. I assumed AUR just ships the latest.

from thirtyfour.

MTRNord avatar MTRNord commented on July 21, 2024

Ok I updated now and geckodriver also gives me now logs which is nice:

➜  ~ geckodriver --log trace --connect-existing --marionette-port 2828
1607722763356	geckodriver	INFO	Listening on 127.0.0.1:4444
1607722770530	webdriver::server	DEBUG	-> POST /session {"capabilities":{"firstMatch":[{}],"alwaysMatch":{"browserName":"firefox"}},"desiredCapabilities":{"browserName":"firefox"}}
1607722770530	geckodriver::marionette	DEBUG	Waiting 60s to connect to browser on 127.0.0.1:2828
1607722770532	geckodriver::marionette	DEBUG	Connection to Marionette established on 127.0.0.1:2828.
1607722770547	webdriver::server	DEBUG	<- 200 OK {"value":{"sessionId":"4d215548-ca6f-4a25-860c-a98e6bab5037","capabilities":{"acceptInsecureCerts":false,"browserName":"firefox","browserVersion":"83.0","moz:accessibilityChecks":false,"moz:buildID":"20201126174332","moz:geckodriverVersion":"0.28.0","moz:headless":false,"moz:processID":73046,"moz:profile":"/home/marcel/.mozilla/firefox/j29h01b5.default-release","moz:shutdownTimeout":60000,"moz:useNonSpecCompliantPointerOrigin":false,"moz:webdriverClick":true,"pageLoadStrategy":"normal","platformName":"linux","platformVersion":"5.9.11-3-MANJARO","rotatable":false,"setWindowRect":true,"strictFileInteractability":false,"timeouts":{"implicit":0,"pageLoad":300000,"script":30000},"unhandledPromptBehavior":"dismiss and notify"}}}
1607722770549	webdriver::server	DEBUG	-> POST /session/4d215548-ca6f-4a25-860c-a98e6bab5037/timeouts {"script":60000,"pageLoad":60000,"implicit":30000}
1607722770552	webdriver::server	DEBUG	<- 200 OK {"value":null}
1607722770552	webdriver::server	DEBUG	-> GET /session/4d215548-ca6f-4a25-860c-a98e6bab5037/title
1607722770554	webdriver::server	DEBUG	<- 200 OK {"value":"command line - How to make a file (e.g. a .sh script) executable, so it can be run from a terminal - Ask Ubuntu"}
1607722770555	webdriver::server	DEBUG	-> GET /session/4d215548-ca6f-4a25-860c-a98e6bab5037/window
1607722770555	webdriver::server	DEBUG	<- 200 OK {"value":"207"}
1607722770556	webdriver::server	DEBUG	-> GET /session/4d215548-ca6f-4a25-860c-a98e6bab5037/window/handles
1607722770556	webdriver::server	DEBUG	<- 500 Internal Server Error {"value":{"error":"unknown error","message":"TypeError: browser.browsingContext is null","stacktrace":"GeckoDriver.prototype.getIdForBrowser@chrome://marionette/content/driver.js:1398:15\nget@chrome://marionette/content/driver.js:239:28\nGeckoDriver.prototype.getWindowHandles@chrome://marionette/content/driver.js:1443:3\ndespatch@chrome://marionette/content/server.js:297:40\nexecute@chrome://marionette/content/server.js:267:16\nonPacket/<@chrome://marionette/content/server.js:240:20\nonPacket@chrome://marionette/content/server.js:241:9\n_onJSONObjectReady/<@chrome://marionette/content/transport.js:504:20\n"}}

Not sure if they are of any help. Still getting the error though

from thirtyfour.

MTRNord avatar MTRNord commented on July 21, 2024

I think I am maybe running into mozilla/geckodriver#1790 Or the fix of it? Atleast it is literally the only page on the internet searchable via google that has the exact error I get.

from thirtyfour.

MTRNord avatar MTRNord commented on July 21, 2024

Uhm after closing a tab I now even get this:

Dec 11 22:45:50.592 ERROR launchpad_sway_controler: Error while executing action: Window not found:
    Status: 404
    Additional info:
        Browsing context has been discarded
        Error: no such window
        Stacktrace:
            WebDriverError@chrome://marionette/content/error.js:181:5
            NoSuchWindowError@chrome://marionette/content/error.js:415:5
            assert.that/<@chrome://marionette/content/assert.js:460:13
            assert.open@chrome://marionette/content/assert.js:168:4
            GeckoDriver.prototype.getTitle@chrome://marionette/content/driver.js:1245:10
            despatch@chrome://marionette/content/server.js:297:40
            execute@chrome://marionette/content/server.js:267:16
            onPacket/<@chrome://marionette/content/server.js:240:20
            onPacket@chrome://marionette/content/server.js:241:9
            _onJSONObjectReady/<@chrome://marionette/content/transport.js:504:20

while there are still plenty of open windows

from thirtyfour.

stevepryde avatar stevepryde commented on July 21, 2024

Getting a 500 does seem like an issue in geckodriver but I'm not sure why you're seeing the error and I'm not. I'm also running Manjaro and firefox 83.0.

You could try asking on the selenium slack channel, there are geckodriver devs on there: https://www.selenium.dev/support/
Otherwise maybe try raising a bug on geckodriver? https://bugzilla.mozilla.org

from thirtyfour.

MTRNord avatar MTRNord commented on July 21, 2024

I will try a bug report at geckodriver itself. Thanks for the help! Looking at numrous restarts of geckdriver it seems to only detect the tab thats active when starting geckodriver. And the fact that I use wayland should I think not make a difference as it hooks into firefox I guess. So maybe this is a geckodriver bug :)

from thirtyfour.

stevepryde avatar stevepryde commented on July 21, 2024

Sorry that link was for marionette. Geckodriver is here: https://github.com/mozilla/geckodriver

from thirtyfour.

stevepryde avatar stevepryde commented on July 21, 2024

Hmm I'm not on wayland (nvidia 👎 ) - wonder if that makes a difference. I started several instances and had multiple tabs in each and it listed all windows and tabs as separate window handles.

from thirtyfour.

stevepryde avatar stevepryde commented on July 21, 2024

I assume you started firefox using firefox -marionette ? I had to do that before geckodriver would recognise the browser

from thirtyfour.

MTRNord avatar MTRNord commented on July 21, 2024

Hm maybe it does. No idea how firefox works. (also it works kinda with nvidia and swaywm :) except vulkan stuff and no official drivers).

Also bugzilla has a geckodriver section. So I guess thats better? Assuming as everything else from mozilla is on bugzilla.

I assume you started firefox using firefox -marionette ? I had to do that before geckodriver would recognise the browser

Yes. Otherwise it wouldn't even start a session :)

from thirtyfour.

stevepryde avatar stevepryde commented on July 21, 2024

Cool - let me know if you find out anything. It's weird how we can both be running Arch (or derivative) and same firefox/geckodriver version but get different results. Something is up with that

from thirtyfour.

MTRNord avatar MTRNord commented on July 21, 2024

Yeah my best bet is with wayland currently. Thats I guess the only major difference :) Also I am wondering if the fix for exactly this n 0.82 causes my issues maybe. But I have no datapoints on previous versions of firefox to compare with.

from thirtyfour.

stevepryde avatar stevepryde commented on July 21, 2024

@MTRNord That Firefox issue is now marked as fixed in Firefox 90. Are you able to confirm?

from thirtyfour.

MTRNord avatar MTRNord commented on July 21, 2024

I will take a look this weekend :)

from thirtyfour.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.