[Python] Selenium: I need help in my code please help me [closed]

Discussão em 'Python' iniciado por Stack, Maio 9, 2025.

  1. Stack

    Stack Membro Participativo

    i am using selenium

    1. open chrome
    2. search keyword example asim jofa
    3. if the website is sponsored
    4. click on 3 dots and extract advertiser name

    but this step is not working my script crashed it's not extracting advertiser name i gave class xpath everthing but not wokring

    please help me guys if you have knowldge about this

    import time
    import undetected_chromedriver as uc
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC

    # Setup undetected Chrome
    options = uc.ChromeOptions()
    options.add_argument("--start-maximized")
    driver = uc.Chrome(options=options)

    # Step 1: Open Google
    driver.get("https://www.google.com")
    time.sleep(2)

    # Step 2: Accept cookies if present
    try:
    agree_btn = WebDriverWait(driver, 5).until(
    EC.element_to_be_clickable((By.XPATH, '//button[.="I agree" or contains(text(), "Accept")]'))
    )
    agree_btn.click()
    time.sleep(1)
    except:
    pass

    # Step 3: Search for keyword
    search_box = driver.find_element(By.NAME, "q")
    search_box.send_keys("asim jofa")
    search_box.send_keys(Keys.RETURN)
    time.sleep(3)

    # Step 4: Click on the first 3-dot menu with aria-label="Why this ad?"
    # try:
    # menu_btn = WebDriverWait(driver, 10).until(
    # EC.element_to_be_clickable((By.XPATH, '//button[@aria-label="Why this ad?"]'))
    # )
    # menu_btn.click()
    # print("✅ Clicked 3-dot menu")
    # except Exception as e:
    # print("❌ Failed to click 3-dot menu:", e)
    # driver.quit()
    # exit()
    iframes = driver.find_elements(By.TAG_NAME, "iframe")
    for iframe in iframes:
    driver.switch_to.frame(iframe)
    try:
    btn = driver.find_element(By.XPATH, '//button[@aria-label="Why this ad?"]')
    btn.click()
    print("✅ Clicked inside iframe")
    break
    except:
    driver.switch_to.default_content()

    # Step 5: Wait for advertiser name to appear
    try:
    advertiser = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CLASS_NAME, "xZhkSd"))
    )
    print("✅ Advertiser Name:", advertiser.text)
    except Exception as e:
    print("❌ Advertiser name not found:", e)

    # Close browser
    driver.quit()


    i want selenium click on3 dots wait ffor the pop up mmodal andd extract advertiser name

    Continue reading...

Compartilhe esta Página