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:
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!
Junaid Amir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.