1

I want to create a script that open links automatically. But i want the program to open the link in a one page pop up.. i tried to open two links one by one in the same tab, but it always open it in a new Tab in the same Browser. this is the code:

import webbrowser
import time

search = input("Enter URL Here : ")
search1 = input("Enter Other URL Here : ")
min = int(input("Enter Wait Time : "))

webbrowser.open(search)
time.sleep(min)
webbrowser.open(search1, new=0)

How can I get the URL to be opened in the same Tab as it should be using new=0?

axchraf
  • 11
  • 2
  • Check these links: [link1](https://stackoverflow.com/q/58565467/16452840),[link2](https://stackoverflow.com/q/40443178/16452840) . – pmadhu Aug 23 '21 at 12:34

1 Answers1

0

After reading some documentation, the Webbrowser module lacks what you asked for.

BUT, seems like Selenium would work for you. I did some research and I ended up in this Reddit post. You would need to install the module plus the driver of the browser you want to use. If you wanna try, this code snippet should work.

from selenium import webdriver

link1="https://www.example.com"
link2="https://www.google.com/"
driver=webdriver.Chrome()
driver.get(link1)
driver.get(link2)
Barraguesh
  • 54
  • 8
  • it opens in the same window but in a second tab, but i want to be opened in the same Tab – axchraf Aug 23 '21 at 14:16
  • @axchraf Sorry I misunderstood your question. That's not possible, the documentation states "If new is 0, the url is opened in the same browser window if possible. If new is 1, a new browser window is opened if possible. If new is 2, a new browser page (“tab”) is opened if possible" https://docs.python.org/3/library/webbrowser.html – Barraguesh Aug 23 '21 at 14:41
  • @axchraf edited the answer to reflect my misunderstanding plus added an alternative using Selenium. – Barraguesh Aug 23 '21 at 15:09
  • i'm trying this code now but it show me an error when i put this code: webdriver.Chrome() this is the msg: start raise WebDriverException( selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home – axchraf Aug 23 '21 at 15:12
  • 1
    Did you install the driver for chrome? https://sites.google.com/chromium.org/driver/ – Jofre Aug 23 '21 at 15:16
  • that's not possible in webbrowser.. i tried selenium webdriver and it worked from the first try – axchraf Aug 24 '21 at 11:44
  • @axchraf if Selenium worked for you, you can mark it as an answer. – Barraguesh Aug 24 '21 at 12:47