0

I want Selenium to check if a window is open in Chrome browser or not, if yes, then open a new tab to the existing window Else, open a new window.

Currently I am using:

from selenium import webdriver
driver = webdriver.Chrome(chromedriver)
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't') 

But this does not open a new tab in an existing open window. Kindly help.

9
  • Does this answer your question? selenium new tab in chrome browser by python webdriver Commented Feb 3, 2020 at 9:32
  • Hi, no.. this opens a new tab, closes -I want to look for is there is already an existing window- if yes, then open a new tab. Commented Feb 3, 2020 at 9:35
  • This is unclear, webdriver.Chrome(chromedriver) will open a new window. Commented Feb 3, 2020 at 9:36
  • To check if there is open windows: selenium-python.readthedocs.io/… Commented Feb 3, 2020 at 9:37
  • Look for driver.window_handles Commented Feb 3, 2020 at 9:37

1 Answer 1

0

If I understood your problem correctly.You would like check if any window is open then you need to open another tab.

For that You need check len(driver.window_handles)>0 then proceed and then using java script executor to open the new tab.

url="https://stackoverflow.com/"
driver=webdriver.Chrome()
if len(driver.window_handles)>0:
    driver.get(url) #proceed with same tab
    driver.execute_script("window.open('{}');".format(url)) #proceed with new tab
Sign up to request clarification or add additional context in comments.

6 Comments

This is opening a new window again, and then a tb.I want a tab on the existing window
Can you provide your code what you have done??.If you see my code its open a window and then check if window exists then navigate url to that window and then open a new tab on existing window.
I ran the exact code.. But it's opening a window first..It's not checking if I already have chrome open..
Without driver how this possible to know whether window open or not.
so, if we run- import webbrowser webbrowser.open('google.com')
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.