0

I am trying to make a python gui application.

What I want to do is to open a web browser by clicking a button. (Tkinter) When the web browser is opened, I do login. After logging it, it will redirect to the page. And that page url will consist of code as a param I need to use later in code.

I used webbrowser.open_new('') to open a web browser. But the limitation was it is only for opening.. there was no way to get the final redirected url I need.

Is there a way I can use to open a web browser and do something on that page and finally get that final url?

I am using python.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • I have also tried to use requests.get('') but it does not open a web browser. If there is a way to open a web browser with that method, please help me to get the redirect_url histories. – Jongwoo Lim Feb 16 '22 at 03:56
  • Do research on [selenium](https://selenium-python.readthedocs.io/). It lets you open a browser and send commands to it. – Bryan Oakley Feb 16 '22 at 04:08
  • @BryanOakley I will do a research. Thank you so much! – Jongwoo Lim Feb 16 '22 at 04:10
  • @BryanOakley. Is this only working for locally? because it assumes that you already have the relevant driver installed. It might work on my machine but with machine from others who have not installed the relevant driver, it would not work? – Jongwoo Lim Feb 16 '22 at 04:28
  • Correct, for it to work on another machine, that machine will require the appropriate driver. – Bryan Oakley Feb 16 '22 at 14:48

1 Answers1

0

There are a few main approaches for automating interactions with browsers:

  1. Telling a program how and what to click, like a human would, sometimes using desktop OS automation tools like Applescript
  2. Parse files that contain browser data (will vary browser to browser, here is Firefox)
  3. Use a tool or library that relies on the WebDriver protocol (e.g. selenium, puppeteer)
  4. Access the local SQLite database of the browser and run queries against it

Sounds like 3 is what you need, assuming you're not against bringing in a new dependency.

Blake Gearin
  • 175
  • 1
  • 10
  • Thank you so much for your answer. I am currently researching selenium. What I have found was that it assumes that the relevant driver is installed (e.g., Chrome driver) on the machine. Does this mean that it only works locally? I am trying to make my app as an executable python program where other people can use as well. However, if their machine does not have browser driver installed, it would not work on them? – Jongwoo Lim Feb 16 '22 at 04:30
  • It means in addition to your browser automation you'll need to script out the driver installation and run that beforehand. Since you can [run shell commands](https://janakiev.com/blog/python-shell-commands/) from inside Python, this shouldn't be too difficult to achieve, aside from maybe permission errors. I know [Selenium can be installed with pip](https://www.geeksforgeeks.org/how-to-install-selenium-in-python/) and pip may already be present on other devices depending on the OS. – Blake Gearin Feb 16 '22 at 04:53