Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 16, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 16, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 16, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 16, 2025

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025

      Minecraft licensing robbed us of this controversial NFL schedule release video

      May 16, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      The power of generators

      May 16, 2025
      Recent

      The power of generators

      May 16, 2025

      Simplify Factory Associations with Laravel’s UseFactory Attribute

      May 16, 2025

      This Week in Laravel: React Native, PhpStorm Junie, and more

      May 16, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025
      Recent

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Selenium + Python: how can we close all opened tabs except the first tab/main tab?

    Selenium + Python: how can we close all opened tabs except the first tab/main tab?

    May 2, 2024

    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])
    ”’

    Source: Read More

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleWhy is White Box Testing Essential in Software Engineering?
    Next Article Get started with Amazon Titan Text Embeddings V2: A new state-of-the-art embeddings model on Amazon Bedrock

    Related Posts

    Security

    Nmap 7.96 Launches with Lightning-Fast DNS and 612 Scripts

    May 17, 2025
    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-4831 – TOTOLINK HTTP POST Request Handler Buffer Overflow Vulnerability

    May 17, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Lessons for Everyone to Learn from My Personal LinkedIn Account Takeover Experience

    Development

    Microsoft Fixes 149 Flaws in Huge April Patch Release, Zero-Days Included

    Development

    Bill Gates says the late Steve Jobs recommended taking LSD to make Microsoft products more appealing — matching Apple’s flair: “Look, I got the wrong batch”

    News & Updates

    The latest Windows 11 update did not end support for older Intel chips. Here’s the source of the confusion.

    News & Updates
    Hostinger

    Highlights

    How to back up (and restore) your saved MacOS passwords

    June 7, 2024

    If you’ve ever lost your passwords, you know that’s a trying situation. Thankfully, MacOS has…

    New Windows 11 reference hints Start Menu Recommendations might be optional

    February 12, 2025

    Interpreting and Improving Optimal Control Problems With Directional Corrections

    April 2, 2025

    CVE-2025-45239 – FoxCMS Directory Traversal Vulnerability

    May 5, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.