如何使用Selenium Python在Facebook广告库的搜索框中发送文本

jdzmm42g  于 2023-02-08  发布在  Python
关注(0)|答案(2)|浏览(121)

我正在尝试使用Selenium的功能设置一个简单的页面浏览自动化脚本。然而,我正处于脚本处理之前xpath未找到问题的步骤中,但现在给了我下一行错误,即需要一个form才能使用我的提交按钮。
下面是我们讨论的函数:

# 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()

理想的情况是:

  • 找到带有XPATH的页面的搜索框
  • 在字段中输入文本“dog”
  • 我提交了实际信息,然后像手动操作一样按下搜索按钮

获取请求的网址正是网页我期待冲浪自动化,主要的搜索框是什么我指的。任何帮助表示感谢。

b91juud3

b91juud31#

使用element_to_be_clickable(),单击搜索框,然后清除搜索框,输入新搜索并单击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)

你需要导入下面的库.

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()
mqxuamgl

mqxuamgl2#

您已经很接近了。Facebook Ad Library上的 * 搜索 * 框是<input>字段。
溶液
理想情况下,要将 * 字符序列 * 发送到 <input> 元素,需要为element_to_be_clickable()引入WebDriverWait,可以使用以下locator strategies之一:

  • 使用 *CSS选择器 *:
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)
  • 使用 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)

*注意:您必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

相关问题