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.