英文:
How to send text within the Search Box of Facebook Ad Library using Selenium Python
问题
我正在尝试使用Selenium的功能来设置一个简单的页面浏览自动化脚本。但是,我遇到了一个问题,脚本已经处理了之前的xpath未找到问题,但现在出现了需要form
来使用我的提交按钮的错误。
以下是相关函数的部分代码:
# 一个使用Selenium来爬取Meta广告库并获取所需广告链接的函数
def get_facebook_ads():
try:
# 初始化浏览器并导航到页面
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")
# 在搜索框中输入关键词
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("未找到表单元素。")
except Exception as e:
print(e)
browser.quit()
理想情况下,这应该只是:
- 使用XPATH查找页面上的搜索框
- 在字段中输入文本'dog'
- 提交实际信息并按照我手动操作的方式按下搜索按钮
GET请求的URL正好是我要自动浏览的页面,主要的搜索框就是我所指的。希望能帮助到您。
英文:
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.
答案1
得分: 1
使用element_to_be_clickable(),点击搜索框,然后清除搜索框并输入新的搜索词,然后点击回车。
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)
需要导入以下库。
from selenium.webdriver.common.keys import Keys
如果要明确点击搜索按钮,可以使用以下代码。
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()
英文:
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()
答案2
得分: 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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论