Python Bot Fails to Select Google Ads and Window Closes Automatically

I’m working on a Python bot that needs to interact with Google Ads, but I’m encountering a couple of issues:

The bot fails to select elements related to Google Ads.
The browser window closes automatically after a short period, which is not intended.
Here’s what I’ve tried so far:

I’ve used various methods from Selenium to locate and interact with elements.
I’ve checked for updates on all relevant libraries and ensured that my browser driver is up to date.
I’ve implemented exception handling to catch any errors that might indicate what’s going wrong.
Despite these efforts, the issues persist. The bot is supposed to run a longer session, and interact with Google Ads without manual intervention. Here is a simplified version of my code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import time
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from urllib.parse import urlparse
def is_allowed_domain(url, allowed_domains):
domain = urlparse(url).netloc
return any(domain.endswith(allowed_domain) for allowed_domain in allowed_domains)
def search_and_save_ads_to_excel(search_keyword, chromedriver_path, allowed_domains, output_excel_path):
# Set Chrome options to open in guest profile
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--guest")
# Set the path to the ChromeDriver executable
service = Service(chromedriver_path)
# Open Chrome browser with the specified options
driver = webdriver.Chrome(service=service, options=chrome_options)
try:
# Maximize the browser window
driver.maximize_window()
# Navigate to Google
driver.get("https://www.google.com")
# Find the search box and enter the search keyword
search_box = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.NAME, "q")))
search_box.send_keys(search_keyword)
search_box.submit()
# Wait for search results to load
WebDriverWait(driver, 20).until(EC.visibility_of_any_elements_located((By.CSS_SELECTOR, ".g")))
# Find all search results
search_results = driver.find_elements(By.CSS_SELECTOR, ".g")
ad_data = []
for result in search_results:
try:
# Look for elements with specific ad attributes (e.g., data-text-ad)
ad_elements = result.find_elements(By.XPATH, ".//div[@data-text-ad='1']")
if ad_elements:
ad_url = result.find_element(By.CSS_SELECTOR, "a").get_attribute("href")
if is_allowed_domain(ad_url, allowed_domains):
ad_title = result.find_element(By.CSS_SELECTOR, "h3").text
ad_data.append({"Title": ad_title, "URL": ad_url})
print(f"Found Ad Title: {ad_title}")
print(f"Found Ad URL: {ad_url}")
# Click on the ad
result.find_element(By.CSS_SELECTOR, "a").click()
WebDriverWait(driver, 20).until(EC.number_of_windows_to_be(2)) # Wait for ad page to open
driver.switch_to.window(driver.window_handles[-1]) # Switch to the new window/tab
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.TAG_NAME, "body"))) # Ensure ad page has loaded
time.sleep(10) # Additional wait for ad page to load
driver.close() # Close the ad page
driver.switch_to.window(driver.window_handles[0]) # Switch back to the main search results window
except (NoSuchElementException, Exception) as e:
print(f"Error processing result: {e}")
# Save ad data to Excel
df = pd.DataFrame(ad_data)
df.to_excel(output_excel_path, index=False)
print(f"Ad data saved to {output_excel_path}")
finally:
driver.quit()
# Example usage (for educational purposes only)
search_keyword = "Sikka Kaamya Greens"
chromedriver_path = r"C:/Users/J.A.S/Desktop/New folder/chromedriver.exe" # Your ChromeDriver path (use raw string literal)
allowed_domains = ["sikka.ixapl.com", "99acres.com"] # List of allowed domains
output_excel_path = "ad_data.xlsx" # Output Excel file path
search_and_save_ads_to_excel(search_keyword, chromedriver_path, allowed_domains, output_excel_path)
</code>
<code>import time import pandas as pd from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import NoSuchElementException from urllib.parse import urlparse def is_allowed_domain(url, allowed_domains): domain = urlparse(url).netloc return any(domain.endswith(allowed_domain) for allowed_domain in allowed_domains) def search_and_save_ads_to_excel(search_keyword, chromedriver_path, allowed_domains, output_excel_path): # Set Chrome options to open in guest profile chrome_options = webdriver.ChromeOptions() chrome_options.add_argument("--guest") # Set the path to the ChromeDriver executable service = Service(chromedriver_path) # Open Chrome browser with the specified options driver = webdriver.Chrome(service=service, options=chrome_options) try: # Maximize the browser window driver.maximize_window() # Navigate to Google driver.get("https://www.google.com") # Find the search box and enter the search keyword search_box = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.NAME, "q"))) search_box.send_keys(search_keyword) search_box.submit() # Wait for search results to load WebDriverWait(driver, 20).until(EC.visibility_of_any_elements_located((By.CSS_SELECTOR, ".g"))) # Find all search results search_results = driver.find_elements(By.CSS_SELECTOR, ".g") ad_data = [] for result in search_results: try: # Look for elements with specific ad attributes (e.g., data-text-ad) ad_elements = result.find_elements(By.XPATH, ".//div[@data-text-ad='1']") if ad_elements: ad_url = result.find_element(By.CSS_SELECTOR, "a").get_attribute("href") if is_allowed_domain(ad_url, allowed_domains): ad_title = result.find_element(By.CSS_SELECTOR, "h3").text ad_data.append({"Title": ad_title, "URL": ad_url}) print(f"Found Ad Title: {ad_title}") print(f"Found Ad URL: {ad_url}") # Click on the ad result.find_element(By.CSS_SELECTOR, "a").click() WebDriverWait(driver, 20).until(EC.number_of_windows_to_be(2)) # Wait for ad page to open driver.switch_to.window(driver.window_handles[-1]) # Switch to the new window/tab WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.TAG_NAME, "body"))) # Ensure ad page has loaded time.sleep(10) # Additional wait for ad page to load driver.close() # Close the ad page driver.switch_to.window(driver.window_handles[0]) # Switch back to the main search results window except (NoSuchElementException, Exception) as e: print(f"Error processing result: {e}") # Save ad data to Excel df = pd.DataFrame(ad_data) df.to_excel(output_excel_path, index=False) print(f"Ad data saved to {output_excel_path}") finally: driver.quit() # Example usage (for educational purposes only) search_keyword = "Sikka Kaamya Greens" chromedriver_path = r"C:/Users/J.A.S/Desktop/New folder/chromedriver.exe" # Your ChromeDriver path (use raw string literal) allowed_domains = ["sikka.ixapl.com", "99acres.com"] # List of allowed domains output_excel_path = "ad_data.xlsx" # Output Excel file path search_and_save_ads_to_excel(search_keyword, chromedriver_path, allowed_domains, output_excel_path) </code>
import time
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from urllib.parse import urlparse

def is_allowed_domain(url, allowed_domains):
    domain = urlparse(url).netloc
    return any(domain.endswith(allowed_domain) for allowed_domain in allowed_domains)

def search_and_save_ads_to_excel(search_keyword, chromedriver_path, allowed_domains, output_excel_path):
    # Set Chrome options to open in guest profile
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("--guest")

    # Set the path to the ChromeDriver executable
    service = Service(chromedriver_path)

    # Open Chrome browser with the specified options
    driver = webdriver.Chrome(service=service, options=chrome_options)

    try:
        # Maximize the browser window
        driver.maximize_window()
      
        # Navigate to Google
        driver.get("https://www.google.com")
      
        # Find the search box and enter the search keyword
        search_box = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.NAME, "q")))
        search_box.send_keys(search_keyword)
        search_box.submit()
      
        # Wait for search results to load
        WebDriverWait(driver, 20).until(EC.visibility_of_any_elements_located((By.CSS_SELECTOR, ".g")))  

        # Find all search results
        search_results = driver.find_elements(By.CSS_SELECTOR, ".g")
        
        ad_data = []
        
        for result in search_results:
            try:
                # Look for elements with specific ad attributes (e.g., data-text-ad)
                ad_elements = result.find_elements(By.XPATH, ".//div[@data-text-ad='1']")
                if ad_elements:
                    ad_url = result.find_element(By.CSS_SELECTOR, "a").get_attribute("href")
                    if is_allowed_domain(ad_url, allowed_domains):
                        ad_title = result.find_element(By.CSS_SELECTOR, "h3").text
                        ad_data.append({"Title": ad_title, "URL": ad_url})
                        print(f"Found Ad Title: {ad_title}")
                        print(f"Found Ad URL: {ad_url}")
                        # Click on the ad
                        result.find_element(By.CSS_SELECTOR, "a").click()
                        WebDriverWait(driver, 20).until(EC.number_of_windows_to_be(2))  # Wait for ad page to open
                        driver.switch_to.window(driver.window_handles[-1])  # Switch to the new window/tab
                        WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.TAG_NAME, "body")))  # Ensure ad page has loaded
                        time.sleep(10)  # Additional wait for ad page to load
                        driver.close()  # Close the ad page
                        driver.switch_to.window(driver.window_handles[0])  # Switch back to the main search results window
            except (NoSuchElementException, Exception) as e:
                print(f"Error processing result: {e}")

        # Save ad data to Excel
        df = pd.DataFrame(ad_data)
        df.to_excel(output_excel_path, index=False)
        print(f"Ad data saved to {output_excel_path}")

    finally:
        driver.quit()

# Example usage (for educational purposes only)
search_keyword = "Sikka Kaamya Greens"
chromedriver_path = r"C:/Users/J.A.S/Desktop/New folder/chromedriver.exe"  # Your ChromeDriver path (use raw string literal)
allowed_domains = ["sikka.ixapl.com", "99acres.com"]  # List of allowed domains
output_excel_path = "ad_data.xlsx"  # Output Excel file path
search_and_save_ads_to_excel(search_keyword, chromedriver_path, allowed_domains, output_excel_path)

Can anyone provide guidance on how to resolve this issue? I have been diligently working on it both day and night without success. Any suggestions would be greatly appreciated. Thank you!

New contributor

Junaid Amir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật