Selenium : I am able to open a new tab with Selenium but cannot make any action on this tab

Viewed 35

Here is what I am trying to do

  1. go to https://accounts.google.com/signup
  2. click on the 'Help'button to open up a new tab,
  3. enter 'Done' in the 'Describe your issue' text (that field is displayed on the tab I just opened)

here is my code :

from selenium import webdriver
import time
import time

from seleniumpagefactory.Pagefactory import PageFactory
from src.pages.home_page import HomePage


class Switch_Me(PageFactory):
    def __init__(self, driver):
        self.driver = driver

    locators = {
        # locators
        "help_btn": ('XPATH', '//*[@id="initialView"]/footer/ul/li[1]/a'),
        "my_problem_on_help_page" : ("XPATH",'//*[@id="search-form"]/input[1]')
    }

    
    def switch(self):
        self.help_btn.click_button()
        time.sleep(2)
        # prints parent window title
        print("Parent window title: " + self.driver.current_url)

        # get current window handle
        p = self.driver.current_window_handle
        print('parent index is :'+ ' '+ p)

        # get all the tabs opened indexes
        chwd = self.driver.window_handles
        print('the tabs indexes openned are: ')
        print (chwd)
        for w in chwd:

            #print (chwd)
            # switch focus to child window
            if (w != p):
                print("I am on the child tab")
                self.driver.switch_to.window(w)
                time.sleep(20)
                self.my_problem_on_help_page.set_text("done")

            break
        time.sleep(0.9)
        #print("Child window title: " + self.driver.title)
        #driver.quit()

Here is my unittest code


from src.pages.sample import Switch_Me
import unittest
from selenium import webdriver
import time

class TestLogin(unittest.TestCase):
    # the function is executed before any test
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.delete_all_cookies()
        self.driver.get("https://accounts.google.com/signup")

        print("I am executed before any test")

    def tearDown(self):
        #self.driver.close()
        print("executed after each test")

    def test_sam(self):
        sa = Switch_Me(self.driver)
        time.sleep(5)
        sa.switch()
        time.sleep(5)
        print("okay switch test")


if __name__ == "__main__":
    unittest.main()

Here is the output when I ran that function (as you can see it doesn't execute the 1 st line of the if statement i.e it cannot switch to the 2 nd tab when I hit the 'help' button) my_outpout

Regards.

1 Answers

I would start by simplifying your code until you get it to work. First thing I would do is ignore the page objects for now and write a simple script that does what you want. Once you have it working, start adding back in complexity until something breaks. That will help you isolate the problem.

I would make sure that your code is giving the site/browser time to open the new tab before moving on. ExpectedConditions has a method new_window_is_opened() that does just that.

Start with simple code. I added some debugging prints.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.maximize_window()
driver.delete_all_cookies()
driver.get("https://accounts.google.com/signup")
wait = WebDriverWait(driver, 10)
window_handles = driver.window_handles # get current window handles for later reference
print("window handles before click: ", window_handles)
driver.find_element(By.LINK_TEXT, "Help").click() # click the Help link
wait.until(EC.new_window_is_opened(window_handles)) # wait for the new window to open
print("window handles after click and wait: ", driver.window_handles)
new_window = [x for x in driver.window_handles if x not in window_handles] # get new window handle
print("new window handle: ", new_window)
driver.switch_to.window(new_window[0])
driver.find_element(By.CSS_SELECTOR, "[name='q']").send_keys("name of issue") # describe the issue
Related