I'm trying to use Selenium and Python to automate searching for ads on the Facebook/Meta Ad Library.
I've tried two possible ways, being the first this code bellow, which I had no luck.
# open up the dropdown
dropdown = driver.find_element_by_css_selector("#content > div > div > div > div._7lcc._7pjc > div > div.i0ppjblf.a5ffu93u.st4mjv7a.iyko1ghw.jfj1vgtg.m83rvdb6.bap0kgut.cti9fvx6.oa8xhnpz.i74zigrp.m98nodcc.chc425dg.e38l7byz.o05r0c4z > div > div > div > div.tds9wb2m.my9zkn4v.pyd2nkot.adkrixjq > div.yukb02kx.duy2mlcu.har4n1i8.cnb3w5di.fo5tpktj.jeth2qeq.j8wh3hf1.e3ydwaff > div > div.a53abz89.rgsc13q7.dfy4e4am.rwb8dzxj.diwav8v6.yukb02kx.apktr6ye.cngk1q1b.iajz466s > div.a53abz89.rgsc13q7.dfy4e4am.diwav8v6.yukb02kx.apktr6ye.tlhxvphw.shyhzvm4 > div > div.rvuf3ahq.nbxbiaw5 > div > div")
dropdown.click()
# select element
item = dropdown.find_element_by_xpath('//*[@id="facebook"]/body/div[5]/div[2]/div[2]/div/div/div/div/div/div/div[2]/div[1]/div/div/div')
item.click()
That was the outcome:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"#content > div > div > div > div._7lcc._7pjc > div > div.i0ppjblf.a5ffu93u.st4mjv7a.iyko1ghw.jfj1vgtg.m83rvdb6.bap0kgut.cti9fvx6.oa8xhnpz.i74zigrp.m98nodcc.chc425dg.e38l7byz.o05r0c4z > div > div > div > div.tds9wb2m.my9zkn4v.pyd2nkot.adkrixjq > div.yukb02kx.duy2mlcu.har4n1i8.cnb3w5di.fo5tpktj.jeth2qeq.j8wh3hf1.e3ydwaff > div > div.a53abz89.rgsc13q7.dfy4e4am.rwb8dzxj.diwav8v6.yukb02kx.apktr6ye.cngk1q1b.iajz466s > div.a53abz89.rgsc13q7.dfy4e4am.diwav8v6.yukb02kx.apktr6ye.tlhxvphw.shyhzvm4 > div > div.rvuf3ahq.nbxbiaw5 > div > div"}
I then tried to use this other code, which partially works. The Ad category button is clicked, but the drop-down option is not. I want to select All ads on the drop-down menu.
The last line of this code fills the search bar. That part was working if I was to click the All ads myself, but is not working anymore, and I don't know why.
# Ad button
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="content"]/div/div/div/div[3]/div/div[2]/div/div/div/div[1]/div[2]/div/div[2]/div[2]/div/div[1]/div/div'))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="facebook"]/body/div[5]/div[2]/div[2]/div/div/div/div/div/div/div[2]/div[1]/div/div/div'))).click()
# Search bar
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="content"]/div/div/div/div[3]/div/div[2]/div/div/div/div[1]/div[2]/div/div[2]/div[3]/div/div/div[1]/div/input'))).send_keys("Type search")
This is my whole code, for now:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("--incognito")
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
# Web page request
driver.get('https://www.facebook.com/ads/library/?active_status=all&ad_type=political_and_issue_ads&country=BR&media_type=all')
driver.maximize_window()
# Ad button
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="content"]/div/div/div/div[3]/div/div[2]/div/div/div/div[1]/div[2]/div/div[2]/div[2]/div/div[1]/div/div'))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="facebook"]/body/div[5]/div[2]/div[2]/div/div/div/div/div/div/div[2]/div[1]/div/div/div'))).click()
# Search bar
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="content"]/div/div/div/div[3]/div/div[2]/div/div/div/div[1]/div[2]/div/div[2]/div[3]/div/div/div[1]/div/input'))).send_keys("Type search")
CodePudding user response:
Go for Relative xpath instead of Absolute xpath. The locators does not highlight any element in the DOM.
It is necessary to find unique locators for Automation.
Refer these links - Link 1 ,Link 2
driver.get("https://www.facebook.com/ads/library/?active_status=all&ad_type=political_and_issue_ads&country=BR&media_type=all")
wait = WebDriverWait(driver,30)
# Click on Ad category
adbtn = wait.until(EC.element_to_be_clickable((By.XPATH,"//div[text()='Ad category']")))
adbtn.click()
# Click on All ads
alladds = wait.until(EC.element_to_be_clickable((By.XPATH,"//span[text()='All ads']")))
alladds.click()
# Search for Chrome
search = wait.until(EC.element_to_be_clickable((By.XPATH,"//input[@placeholder='Search by keyword or advertiser']")))
search.send_keys("Chrome")
# Select option from Suggestions.
chromeoption = wait.until(EC.element_to_be_clickable((By.XPATH,"//button//div[text()='Google Chrome']")))
chromeoption.click()