0

This is my code,

   '''
import time
 
from selenium.webdriver.common.by import By
 
from selenium.webdriver import ActionChains
 
from selenium.webdriver.support import expected_conditions as EC
 
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
num = str(datetime.datetime.now())
driver = webdriver.Firefox(executable_path="C:\\geckodriver.exe")
driver.get("https://my.eventbuizz.com/_super/analytics_requests")
driver.maximize_window()
time.sleep(4)
driver.find_element_by_name("email").send_keys("text")
driver.find_element_by_name("password").send_keys("text")
driver.find_element_by_xpath("//input[@value='login']").click()
time.sleep(5)
driver.find_element_by_xpath("//span[contains(text(),'Google Analytics')]").click()
time.sleep(2)
driver.find_element_by_xpath("//span[contains(text(),'Analytics Requests')]").click()
ActionChains = ActionChains(driver)
time.sleep(2)
 
lists = driver.find_elements_by_xpath("//*[@class= 'gradeA']/td[1]")
for list_item in lists:
    print(len(lists))
    print(type(lists))
    print(list_item.text)
    output = list_item.text
 
    driver.execute_script("window.open('');")
    driver.switch_to.window(driver.window_handles[1])
    time.sleep(2)
    driver.get("https://my.eventbuizz.com/_super/organizerEvent/organizerEventList")
    time.sleep(2)
    driver.find_element_by_xpath("//input[@id='event_name']").send_keys(output)
    time.sleep(2)
    driver.find_element_by_xpath("//input[@value='Search']").click()
    time.sleep(2)
    time.sleep(3)
    driver.find_element_by_xpath("//a[normalize-space()='Auto Login']").click()
    time.sleep(5)
 
    driver.switch_to.window(driver.window_handles[2])
    time.sleep(4)
    driver.find_element_by_xpath("//input[@name='searchText']").send_keys(output)
    driver.find_element_by_xpath("//input[@id='btn_sbt']").click()
    time.sleep(2)
    driver.find_element_by_xpath("//div[@class='img-center']//a").click()
    time.sleep(3)
    event_name = driver.find_element_by_xpath("//input[@id='event_name']").get_attribute('value')
    print("Event Name = " + event_name)
    time.sleep(5)
    a = driver.find_element_by_xpath("//body[1]/section[1]/section[1]/form[1]/div[1]/div[3]/div[3]/div[2]/em[1]").text
    ab = a[8:]
    print("Event URl = " + ab)
 
    driver.execute_script("window.open('');")
    driver.switch_to.window(driver.window_handles[3])
    time.sleep(2)
    driver.get("https://analytics.google.com/analytics/web/#/a213435779w295029133p256179771/admin")
    time.sleep(4)
    email_phone = driver.find_element_by_xpath("//input[@id='identifierId']")
    time.sleep(2)
    email_phone.send_keys("eb.march.eventbuizz2021@gmail.com")
    driver.find_element_by_id("identifierNext").click()
    password = WebDriverWait(driver, 5).until(
        EC.element_to_be_clickable((By.XPATH, "//input[@name='password']")))
    password.send_keys("text")
    driver.find_element_by_id("passwordNext").click()
    time.sleep(20)
    # driver.find_element_by_xpath("//span[@class='ga-dialog-title-close']").click()
    # time.sleep(6)
    driver.find_element_by_xpath("//span[contains(text(),'Create Property')]").click()
    time.sleep(4)
    driver.find_element_by_xpath("//input[@id='name']").click()
    time.sleep(4)
    driver.find_element_by_xpath("//input[@id='name']").send_keys(event_name)
    time.sleep(4)
 
    countries = driver.find_elements_by_css_selector("[class='menu-open-button-label-text']")
    for country in countries:
        if country.text == "United Kingdom":
            country.click()
            break
    time.sleep(2)
    country_name = driver.find_elements_by_css_selector("[class='option-container ng-star-inserted']")
    for count in country_name:
        if count.text == "Denmark":
            count.click()
            break
 
    time.sleep(2)
    amount_code = driver.find_elements_by_xpath("//div[@class='option-item-wrapper ng-star-inserted']")
    for code in amount_code:
        if code.text == "Danish Krone (DKK DKK)":
            code.click()
            break
 
    time.sleep(2)
    driver.find_element_by_xpath("//span[contains(text(),'Show advanced options')]").click()
    time.sleep(2)
    driver.find_element_by_css_selector("[class='mat-slide-toggle-thumb']").click()
    time.sleep(2)
    end = driver.find_element_by_xpath("//label[contains(text(),'Website URL')]")
    driver.execute_script("document.body.scrollIntoView();", end)
    time.sleep(2)
    driver.find_element_by_xpath("//input[@type='url']").send_keys(ab)
 
    driver.find_element_by_xpath("//label[@for='mat-radio-8-input']//span[@class='mat-radio-label-content']").click()
    time.sleep(2)
    driver.find_element_by_xpath("//span[contains(text(),'Next')]").click()
    time.sleep(2)
 
    driver.find_element_by_xpath("//label[@for='mat-radio-2-input']//span[@class='mat-radio-label-content']").click()
 
    time.sleep(3)
    driver.find_element_by_xpath("//label[@for='mat-checkbox-2-input']//span[@class='mat-checkbox-inner-container']").click()
    time.sleep(2)
    driver.find_element_by_xpath("//span[normalize-space()='Create']").click()
    time.sleep(20)
    driver.switch_to.frame("galaxyIframe")
    time.sleep(5)
    track = driver.find_element_by_xpath("//div[@class='_GACjb']").text
    print(print("Tracking ID = " + track))
 
    driver.switch_to.default_content()
    time.sleep(2)
    driver.find_element_by_xpath("//mat-icon[contains(text(),'arrow_back')]").click()
    time.sleep(2)
    driver.find_element_by_xpath("//span[contains(text(),'View Access Management')]").click()
    time.sleep(8)
    driver.find_element_by_xpath("//button[@aria-label='Add access to new users']//span[@class='mat-button-wrapper']").click()
    time.sleep(3)
    driver.find_element_by_xpath("//button[normalize-space()='Add users']").click()
    time.sleep(3)
    driver.find_element_by_id("mat-chip-list-input-0").send_keys(
        "analytics47@api-project-770804776074.iam.gserviceaccount.com")
    time.sleep(3)
    driver.find_element_by_xpath("//span[normalize-space()='Add']").click()
    time.sleep(5)
    driver.find_element_by_xpath("//mat-icon[normalize-space()='close']").click()
    time.sleep(3)
    driver.find_element_by_xpath("//span[contains(text(),'View Settings')]").click()
    time.sleep(3)
    view_id = driver.find_element_by_css_selector("[class='content view-id']").text
    print("View ID =" + view_id)
    time.sleep(3)
    driver.execute_script("window.open('');")
    driver.switch_to.window(driver.window_handles[4])
    time.sleep(2)
    driver.get("https://my.eventbuizz.com/_admin/add_google_analytics")
    time.sleep(4)
    driver.find_element_by_xpath("//input[@placeholder='Google analytics email']").clear()
    time.sleep(2)
    driver.find_element_by_xpath("//input[@placeholder='Google analytics email']").send_keys(
        " analytics47@api-project-770804776074.iam.gserviceaccount.com")
    time.sleep(1)
    driver.find_element_by_xpath("//input[@placeholder='Google Analytics Code']").clear()
    time.sleep(2)
    driver.find_element_by_xpath("//input[@placeholder='Google Analytics Code']").send_keys(track)
    time.sleep(1)
    driver.find_element_by_xpath("//input[@placeholder='Google profile ID']").clear()
    time.sleep(2)
    driver.find_element_by_xpath("//input[@placeholder='Google profile ID']").send_keys(view_id)
    time.sleep(2)
    driver.find_element_by_xpath("//input[@class='save-event']").click()
    time.sleep(4)
    driver.find_element_by_xpath("//a[@class='btn btn-sm log-out text-right']").click()
    time.sleep(3)
    driver.switch_to.window(driver.window_handles[5])
    time.sleep(4)
    driver.find_element_by_xpath("//label[normalize-space()='Statistics']").click()
    time.sleep(2)
    driver.find_element_by_xpath("//a[normalize-space()='Accept all cookies']").click()
 
    time.sleep(5)
    driver.switch_to.window(driver.window_handles[3])
    driver.find_element_by_xpath("//md-icon[@md-font-icon='realtime-icon']").click()
    time.sleep(3)
    driver.find_element_by_xpath("//span[@class='ga-nav-link-label ga-nav-link-no-icon']").click()
    time.sleep(3)
    print("************************ Google analytics added successfully **************************** ")
 
    with open('Text.txt', mode='w') as f:
        f.write("Event_name" + "          " + "Event_Tracking_ID" + "\n")
        for post in event_name:
            f.write(post)
        f.write("          ")
        for post1 in track:
            f.write(post1)
        f.write("\n")
    driver.switch_to.window(driver.window_handles[0])
    '''

1 Answer 1

0

You can try something like this,

driver.switch_to.window(driver.window_handles[5]).close()
driver.switch_to.window(driver.window_handles[4]).close()
driver.switch_to.window(driver.window_handles[3]).close()
driver.switch_to.window(driver.window_handles[2]).close()
driver.switch_to.window(driver.window_handles[1]).close()

Basically, switch to the particular window that you want to close and then call driver.close() method on it.

You can put the above in a loop. It would look something like:

for x in range(1, 6):
    driver.switch_to.window(driver.window_handles[x]).close()
4
  • Is it is possible to handle it with a loop .? Dec 14, 2021 at 5:55
  • Yes. Put the statement inside a for loop and replace the handle id with the loop counter. Dec 14, 2021 at 7:37
  • can you write a code please @IAmMilinPatel Dec 14, 2021 at 8:26
  • I've added a sample code with For loop in the answer. That should work. If it does, you can mark the answer as accepted. Dec 14, 2021 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.