Here is what I am trying to do
- go to https://accounts.google.com/signup
- click on the 'Help'button to open up a new tab,
- 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)

Regards.