0

I created a program in python selenium which does an action when you go on a specific url on selenium (with chrome driver). The problem is that only the first tab detects the url and if any other tab is used to visit the url it will not be detected.

my code to detect the url:


driver = webdriver.Chrome()
driver.get('https://www.google.com')

paypal_url = "paypal.com"
google_url = "google.com"


while True:
    url = driver.current_url
    if paypal_url in url and "signin" in url:
        print("Connected on paypal !")
        sleep(1)
        do_action()
        break
    elif google_url in url and "signin" in url:
        print("Connected on google !")
        sleep(1)
        do_action()
        break
    else:
        sleep(1)

My question is:

How can I detect when the user goes on a specific url in any tabs (not only the first one)

Sarlay
  • 15
  • 4
  • did you assign the same value to both `paypal_url` and `google_url` in the script? – liamsuma Aug 11 '20 at 14:22
  • So, you run the python script, and use the automated chrome that this opens to open other tabs, and desire to be able to detect URLs in all open tabs, correct? – Sarwagya Aug 11 '20 at 14:27
  • Try looking at: https://stackoverflow.com/questions/46416852/get-urls-of-all-open-tabs-using-python – Sarwagya Aug 11 '20 at 14:41
  • I looked and this could work but actually thie user can't use the browser if the tab changes constantly. Is there any way to do this task in background or without changing the tab for the user (only for the program) ? – Sarlay Aug 11 '20 at 14:53
  • If it's not possible to get the url of all tabs, is there a way to just get the url of the displayed tab (inside chromedriver) ? – Sarlay Aug 11 '20 at 17:20

1 Answers1

0

You could use the following for-loop:

urls = []
for i in range(0, len(driver.window_handles), 1):
  driver.switch_to.window(driver.window_handles[i])
  urls.append(driver.current_url)
Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49
benicamera
  • 750
  • 1
  • 7
  • 24