How to make a Loop with Tabs in Selenium Python

Issue

I am trying to do an infinite loop. That makes the search in the Three Tabs an Element in xpath, if the element is found press click and if it is not found go to the next tab.

In the Part of #Important! Find element and click. I need to do the loop, I still don’t understand how to correctly make a loop that I check in each tab. 🙁

# Librerías
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import sys

#Open Three URL

driver.execute_script('window.open("https://locahost/login.php", "_blank", "resizable=yes, scrollbars=yes, titlebar=yes, width=500, height=500, top=0, left=000");')

driver.switch_to.window(driver.window_handles[0])

driver.execute_script('window.open("http://locahost/login.php", "_blank", "resizable=yes, scrollbars=yes, titlebar=yes, width=500, height=500, top=0, left=500");')

driver.switch_to.window(driver.window_handles[1])

driver.execute_script('window.open("http://locahost/login.php", "_blank", "resizable=yes, scrollbars=yes, titlebar=yes, width=500, height=500, top=0, left=1000");')

driver.switch_to.window(driver.window_handles[2])

#Login in three tabs 

driver.switch_to.window(driver.window_handles[0])

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,
                                      'button.align-right secondary slidedown-button'.replace(' ', '.'))))\
    .click()

inputElement = driver.find_element_by_id("username")
inputElement.send_keys('Admin')

time.sleep(1)

inputElement = driver.find_element_by_id("password")
inputElement.send_keys('12345678')

element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="login"]/div[4]/button')))
element.click();

driver.switch_to.window(driver.window_handles[1])
inputElement = driver.find_element_by_id("username")
inputElement.send_keys('Admin')

time.sleep(1)

inputElement = driver.find_element_by_id("password")
inputElement.send_keys('12345678')

element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="login"]/div[4]/button')))
element.click();

time.sleep(1)

driver.switch_to.window(driver.window_handles[2])
inputElement = driver.find_element_by_id("username")
inputElement.send_keys('Admin')

time.sleep(1)

inputElement = driver.find_element_by_id("password")
inputElement.send_keys('12345678')

element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="login"]/div[4]/button')))
element.click();

time.sleep(1)

#Important! Find element and click

driver.switch_to.window(driver.window_handles[0])

element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="fragment-pane"]/div/div/div[1]/div[1]/a/div')))
element.click();

driver.switch_to.window(driver.window_handles[1])

element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="fragment-pane"]/div/div/div[1]/div[1]/a/div')))
element.click();

driver.switch_to.window(driver.window_handles[2])

element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="fragment-pane"]/div/div/div[1]/div[1]/a/div')))
element.click();

Solution

You can refer this – How to loop through multiple Chrome browser tabs with Selenium and Python?

Can also include try,except block so that if the element is not found or Clickable, the execution does not stop. The code might look like this

    xpath = "//*[@id='fragment-pane']/div/div/div[1]/div[1]/a/div"
    count = len(driver.window_handles)

    for i in range(count):
        driver.switch_to.window(driver.window_handles[i])
        try:
            element = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, xpath)))
            element.click();
        except:
            pass

Answered By – pmadhu

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published