8.9 C
New York
Wednesday, March 26, 2025

automated testing – Selenium + Python: how can we shut all opened tabs besides the primary tab/principal tab?


That is my code,

   '''
import time
 
from selenium.webdriver.widespread.by import By
 
from selenium.webdriver import ActionChains
 
from selenium.webdriver.help import expected_conditions as EC
 
from selenium import webdriver
from selenium.webdriver.help.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("electronic mail").send_keys("textual content")
driver.find_element_by_name("password").send_keys("textual content")
driver.find_element_by_xpath("//enter[@value="login"]").click on()
time.sleep(5)
driver.find_element_by_xpath("//span[contains(text(),'Google Analytics')]").click on()
time.sleep(2)
driver.find_element_by_xpath("//span[contains(text(),'Analytics Requests')]").click on()
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(sort(lists))
    print(list_item.textual content)
    output = list_item.textual content
 
    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("//enter[@id='event_name']").send_keys(output)
    time.sleep(2)
    driver.find_element_by_xpath("//enter[@value="Search"]").click on()
    time.sleep(2)
    time.sleep(3)
    driver.find_element_by_xpath("//a[normalize-space()='Auto Login']").click on()
    time.sleep(5)
 
    driver.switch_to.window(driver.window_handles[2])
    time.sleep(4)
    driver.find_element_by_xpath("//enter[@name="searchText"]").send_keys(output)
    driver.find_element_by_xpath("//enter[@id='btn_sbt']").click on()
    time.sleep(2)
    driver.find_element_by_xpath("//div[@class="img-center"]//a").click on()
    time.sleep(3)
    event_name = driver.find_element_by_xpath("//enter[@id='event_name']").get_attribute('worth')
    print("Occasion Identify = " + event_name)
    time.sleep(5)
    a = driver.find_element_by_xpath("//physique[1]/part[1]/part[1]/type[1]/div[1]/div[3]/div[3]/div[2]/em[1]").textual content
    ab = a[8:]
    print("Occasion 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/net/#/a213435779w295029133p256179771/admin")
    time.sleep(4)
    email_phone = driver.find_element_by_xpath("//enter[@id='identifierId']")
    time.sleep(2)
    email_phone.send_keys("[email protected]")
    driver.find_element_by_id("identifierNext").click on()
    password = WebDriverWait(driver, 5).till(
        EC.element_to_be_clickable((By.XPATH, "//enter[@name="password"]")))
    password.send_keys("textual content")
    driver.find_element_by_id("passwordNext").click on()
    time.sleep(20)
    # driver.find_element_by_xpath("//span[@class="ga-dialog-title-close"]").click on()
    # time.sleep(6)
    driver.find_element_by_xpath("//span[contains(text(),'Create Property')]").click on()
    time.sleep(4)
    driver.find_element_by_xpath("//enter[@id='name']").click on()
    time.sleep(4)
    driver.find_element_by_xpath("//enter[@id='name']").send_keys(event_name)
    time.sleep(4)
 
    international locations = driver.find_elements_by_css_selector("[class="menu-open-button-label-text"]")
    for nation in international locations:
        if nation.textual content == "United Kingdom":
            nation.click on()
            break
    time.sleep(2)
    country_name = driver.find_elements_by_css_selector("[class="option-container ng-star-inserted"]")
    for depend in country_name:
        if depend.textual content == "Denmark":
            depend.click on()
            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.textual content == "Danish Krone (DKK DKK)":
            code.click on()
            break
 
    time.sleep(2)
    driver.find_element_by_xpath("//span[contains(text(),'Show advanced options')]").click on()
    time.sleep(2)
    driver.find_element_by_css_selector("[class="mat-slide-toggle-thumb"]").click on()
    time.sleep(2)
    finish = driver.find_element_by_xpath("//label[contains(text(),'Website URL')]")
    driver.execute_script("doc.physique.scrollIntoView();", finish)
    time.sleep(2)
    driver.find_element_by_xpath("//enter[@type="url"]").send_keys(ab)
 
    driver.find_element_by_xpath("//label[@for="mat-radio-8-input"]//span[@class="mat-radio-label-content"]").click on()
    time.sleep(2)
    driver.find_element_by_xpath("//span[contains(text(),'Next')]").click on()
    time.sleep(2)
 
    driver.find_element_by_xpath("//label[@for="mat-radio-2-input"]//span[@class="mat-radio-label-content"]").click on()
 
    time.sleep(3)
    driver.find_element_by_xpath("//label[@for="mat-checkbox-2-input"]//span[@class="mat-checkbox-inner-container"]").click on()
    time.sleep(2)
    driver.find_element_by_xpath("//span[normalize-space()='Create']").click on()
    time.sleep(20)
    driver.switch_to.body("galaxyIframe")
    time.sleep(5)
    monitor = driver.find_element_by_xpath("//div[@class="_GACjb"]").textual content
    print(print("Monitoring ID = " + monitor))
 
    driver.switch_to.default_content()
    time.sleep(2)
    driver.find_element_by_xpath("//mat-icon[contains(text(),'arrow_back')]").click on()
    time.sleep(2)
    driver.find_element_by_xpath("//span[contains(text(),'View Access Management')]").click on()
    time.sleep(8)
    driver.find_element_by_xpath("//button[@aria-label="Add access to new users"]//span[@class="mat-button-wrapper"]").click on()
    time.sleep(3)
    driver.find_element_by_xpath("//button[normalize-space()='Add users']").click on()
    time.sleep(3)
    driver.find_element_by_id("mat-chip-list-input-0").send_keys(
        "[email protected]")
    time.sleep(3)
    driver.find_element_by_xpath("//span[normalize-space()='Add']").click on()
    time.sleep(5)
    driver.find_element_by_xpath("//mat-icon[normalize-space()='close']").click on()
    time.sleep(3)
    driver.find_element_by_xpath("//span[contains(text(),'View Settings')]").click on()
    time.sleep(3)
    view_id = driver.find_element_by_css_selector("[class="content view-id"]").textual content
    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("//enter[@placeholder="Google analytics email"]").clear()
    time.sleep(2)
    driver.find_element_by_xpath("//enter[@placeholder="Google analytics email"]").send_keys(
        " [email protected]")
    time.sleep(1)
    driver.find_element_by_xpath("//enter[@placeholder="Google Analytics Code"]").clear()
    time.sleep(2)
    driver.find_element_by_xpath("//enter[@placeholder="Google Analytics Code"]").send_keys(monitor)
    time.sleep(1)
    driver.find_element_by_xpath("//enter[@placeholder="Google profile ID"]").clear()
    time.sleep(2)
    driver.find_element_by_xpath("//enter[@placeholder="Google profile ID"]").send_keys(view_id)
    time.sleep(2)
    driver.find_element_by_xpath("//enter[@class="save-event"]").click on()
    time.sleep(4)
    driver.find_element_by_xpath("//a[@class="btn btn-sm log-out text-right"]").click on()
    time.sleep(3)
    driver.switch_to.window(driver.window_handles[5])
    time.sleep(4)
    driver.find_element_by_xpath("//label[normalize-space()='Statistics']").click on()
    time.sleep(2)
    driver.find_element_by_xpath("//a[normalize-space()='Accept all cookies']").click on()
 
    time.sleep(5)
    driver.switch_to.window(driver.window_handles[3])
    driver.find_element_by_xpath("//md-icon[@md-font-icon='realtime-icon']").click on()
    time.sleep(3)
    driver.find_element_by_xpath("//span[@class="ga-nav-link-label ga-nav-link-no-icon"]").click on()
    time.sleep(3)
    print("************************ Google analytics added efficiently **************************** ")
 
    with open('Textual content.txt', mode="w") as f:
        f.write("Event_name" + "          " + "Event_Tracking_ID" + "n")
        for publish in event_name:
            f.write(publish)
        f.write("          ")
        for post1 in monitor:
            f.write(post1)
        f.write("n")
    driver.switch_to.window(driver.window_handles[0])
    '''

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles