1

I'm trying to setup a simple page surfing automation script using Selenium's functionality. However I'm at a step where the script has handled a previous xpath not found issue, but now gave me the next lines error of needing a form to utilize my submit button.

Here is the piece of function in question:

# A function to utilize Selenium to crawl the Meta Ads Library and grab needed ads links 
def get_facebook_ads():

try:
    # Initialize the browser and navigate to the page
    browser = webdriver.Chrome(executable_path="C:\\Users\\S\\OneDrive\\Programming\\Learning-Projects\\chromedriver.exe")
    browser.get("https://www.facebook.com/ads/library/?active_status=active&ad_type=all&country=ALL&q=%22%20%22&sort_data[direction]=desc&sort_data[mode]=relevancy_monthly_grouped&search_type=keyword_exact_phrase&media_type=all&content_languages[0]=en")

    # Enter a keyword in the search box
    search_box = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, "//input[@placeholder='Search by keyword or advertiser']")))
    search_box.send_keys("dog")
    try:
        form = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, "//form")))
        form.submit()
    except:
        print("Form element not found.")
except Exception as e:
    print(e)
    browser.quit()

Ideally this should just;

  • find the search box of the page with the XPATH
  • input the text 'dog' into the field
  • submit the actual information and press the search button as I would manually

The get request url is exactly the page im looking to surf automate, the main search box is what im referring to. Any help appreciated.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Saad.A
  • 33
  • 1
  • 9

2 Answers2

1

Use element_to_be_clickable(), click on the search box and then clear the search box and enter new search and click enter.

browser.get("https://www.facebook.com/ads/library/?active_status=all&ad_type=all&country=ALL&q=%22dogs%22&sort_data[direction]=desc&sort_data[mode]=relevancy_monthly_grouped&search_type=keyword_exact_phrase&media_type=all")
search=WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,"//input[@placeholder='Search by keyword or advertiser']")))
search.click()
search.clear()
search.send_keys("cats"+Keys.ENTER)

you need to import below library.

from selenium.webdriver.common.keys import Keys

If you want to explicitly click on search button here you go.

browser.get("https://www.facebook.com/ads/library/?active_status=all&ad_type=all&country=ALL&q=%22dogs%22&sort_data[direction]=desc&sort_data[mode]=relevancy_monthly_grouped&search_type=keyword_exact_phrase&media_type=all")
search=WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,"//input[@placeholder='Search by keyword or advertiser']")))
search.click()
search.clear()
search.send_keys("cats")
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,"//div[@aria-label='Search']"))).click()
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • 1
    Thanks for such a clear response. However I can't help but notice the browser it opens, completes the action in microseconds, i barely get to see the output of the search command it performs. Changing the webdriverwait counter from 10 to 20 didn't do anything. Got any idea? – Saad.A Feb 06 '23 at 23:59
  • 1
    Import time library and after search click use time.sleep(10) this will pause for 10 seconds and you can see the search command performed. You can increase the time as long you want. – KunduK Feb 07 '23 at 00:03
1

You were pretty close. The Search box on Facebook Ad Library is an <input> field.


Solution

Ideally to send a character sequence to an <input> element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    driver.get('https://www.facebook.com/ads/library/?active_status=active&ad_type=all&country=ALL&q=%22%20%22&search_type=keyword_exact_phrase&media_type=all&content_languages[0]=en')
    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[placeholder='Search by keyword or advertiser']")))
    element.click()
    element.clear()
    element.send_keys("dog" +Keys.RETURN)
    
  • Using XPATH:

    driver.get('https://www.facebook.com/ads/library/?active_status=active&ad_type=all&country=ALL&q=%22%20%22&search_type=keyword_exact_phrase&media_type=all&content_languages[0]=en')
    element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@placeholder='Search by keyword or advertiser']")))
    element.click()
    element.clear()
    element.send_keys("dog" +Keys.RETURN)
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352