代码之家  ›  专栏  ›  技术社区  ›  Jonathan Odamah

如何创建通用XPath来向Facebook组中的所有成员发送消息?

  •  0
  • Jonathan Odamah  · 技术社区  · 10 月前

    我正在开发一个使用Selenium向Facebook群组成员发送消息的Facebook自动化脚本。我的脚本成功登录到Facebook并导航到成员部分。然而,我遇到了一个问题,即脚本无法一致地找到每个成员的“消息”按钮或与之交互,因为XPath似乎因成员配置文件而异。

    以下是试图识别成员并向他们发送消息的代码部分:

    def send_messages_to_members(driver, message_template, amount_of_leads, delay_between_messages):
        try:
            navigate_to_group(driver)
    
            action = ActionChains(driver)
    
            # Scroll to load all members
            driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            time.sleep(2)
    
            member_elements = driver.find_elements(By.XPATH, "//*[contains(@href, '/profile.php?id=') or contains(@href, '/people/')]")
            member_elements = member_elements[:amount_of_leads]
    
            for index, member in enumerate(member_elements):
                try:
                    # Hover over member's name to trigger the popup
                    action.move_to_element(member).perform()
                    time.sleep(2)  # Wait for the popup to appear
    
                    # Avoid messaging Admins, Moderators, or yourself
                    if 'Admins & Moderators' in member.text or 'Your Name' in member.text:
                        print("Skipping {}".format(member.text))
                        continue
    
                    try:
                        # Generic XPath for the message button in the popup
                        message_button = driver.find_element(By.XPATH, "//div[contains(@aria-label, 'Message')]")
                        message_button.click()
                        time.sleep(5)  # Wait for the message interface to load
    
                        try:
                            # Find the message box and send the message
                            message_box = driver.find_element(By.CSS_SELECTOR, "div[role='textbox']")
                            message_box.click()
                            message_box.send_keys(message_template)
                            message_box.send_keys(Keys.RETURN)
                            time.sleep(delay_between_messages)  # Wait before moving to the next user
                            print("Message sent successfully to user {}.".format(index + 1))
                        except Exception as e:
                            print("Failed to send message to user {}: {}".format(index + 1, e))
                            continue
    
                        # Close the message interface
                        close_button = driver.find_element(By.XPATH, "//div[contains(@aria-label, 'Close')]")
                        close_button.click()
                        time.sleep(2)  # Wait for the interface to close
    
                    except Exception as e:
                        print("Failed to click on the message button for user {}: {}".format(index + 1, e))
                        continue
    
                    # Scroll down the member list
                    driver.execute_script("arguments[0].scrollIntoView();", member)
                    time.sleep(2)  # Wait for the scrolling action
    
                except Exception as e:
                    print("Error during message sending: {}".format(e))
                    continue
    
            return "Successfully sent messages to {} users.".format(len(member_elements))
        except Exception as e:
            return "Failed: {}".format(e)
    

    脚本有时无法使用当前XPath定位消息按钮:

    message_button=driver.find_element(By.XPATH,“//div[包含(@aria-label,'message')]”)

    这似乎是因为成员元素的结构可能因用户而异。是否有更通用的XPath可用于可靠地查找并单击任何成员的“消息”按钮?或者,是否有其他方法来确保脚本始终能找到正确的按钮?任何建议或见解都将不胜感激!

    0 回复  |  直到 10 月前