如何使用Python在Wordpress上发布新帖子?

问题描述 投票:0回答:1

我正在尝试使用 Python 在 wordpress.com 上发布和编辑新帖子?

我可以使用我的用户名和密码登录,还可以访问使用以下 SELENIUM PYTHON 代码加载的新 wordpress.com 发布页面。

但是由于某种原因我无法选择标题字段...有没有更简单的方法来获取 CSS 选择器? 或元素ID? 也许它是动态创建的并且不会“正常”显示? 这是代码中的错误吗?

title_field = WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'h1.wp-block')))
        enter_text(title_field, title)

这是我正在使用的完整Python代码...

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

def list_my_sites(driver):
    try:
        # Navigate to "My Sites" after successful login
        my_sites_link = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, "a.masterbar__item:nth-child(2)"))
        )
        my_sites_link.click()

        # Wait for the table to be present (adjust timeout as needed)
        table_body = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, ".dataviews-view-table > tbody"))  # Use a more general selector for the table body
        )

        # Get all rows in the table body
        rows = table_body.find_elements(By.TAG_NAME, "tr")


        first_column_data = []
        for row in rows:
            try:  # Handle potential errors if a row doesn't have enough columns
                first_cell = row.find_element(By.CSS_SELECTOR, "td:nth-child(1)")  # Get the first cell (column) in each row
                first_column_data.append(first_cell.text) # extract the text from that cell
            except: #NoSuchElementException:
                print("Skipping a row, its missing a first cell")

        print (first_column_data)

    except Exception as e:
        print(f"Error listing sites: {e}")


def create_and_publish_post(driver, title, content, tags=None, category=None, featured_image_path=None):
    def enter_text(element, text):  # Helper function for reliable text entry
        element.click()
        action = webdriver.ActionChains(driver)
        action.send_keys(text).perform()

    try:
        # 1. Navigate to "Add New Post"
        driver.get(website + "/wp-admin/post-new.php")  # Or your "Add New" URL

        # 2. Enter title
        title_field = WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'h1.wp-block')))
        enter_text(title_field, title)

        # 3. Enter content (Gutenberg)
        content_field = WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".block-editor-rich-text__editable")))
        enter_text(content_field, content)


        # 4. Add tags (if provided)
        if tags:
            tag_field = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "tags-input")))
            for tag in tags:
                enter_text(tag_field, tag)
                tag_field.send_keys(Keys.ENTER)

        # 5. Add category (if provided)  # New feature!
        if category:
            category_field = WebDriverWait(driver, 10).until(
                EC.presence_of_element_located((By.ID, "category-add-toggle")) # This will vary a lot across sites, INSPECT YOURS.
            )
            category_field.click() # click on the button first

            category_input_field = WebDriverWait(driver, 10).until(
                EC.presence_of_element_located((By.ID, "new-category-name"))  # sometimes there is an input for adding new categories
            )

            enter_text(category_input_field, category) # type in the category you want
            category_input_field.send_keys(Keys.ENTER) # select the newly added category


        # 6. Add featured image (if provided) # New feature!
        if featured_image_path:
            featured_image_button = WebDriverWait(driver, 10).until(
                EC.presence_of_element_located((By.CSS_SELECTOR, ".editor-post-featured-image__toggle")) #  Click to open the Featured Image settings
            ).click()


            upload_button = WebDriverWait(driver, 10).until(
                EC.presence_of_element_located((By.CSS_SELECTOR, ".components-button.editor-post-featured-image__upload-button"))  #  This one is tricky, it tends to have a dynamic ID, better to use a more broad selector
            )
            upload_button.send_keys(featured_image_path)  # Send the path directly to the upload button



        # 7. Publish
        WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".editor-post-publish-panel__toggle"))).click()  # Open publish panel
        WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".editor-post-publish-button"))).click()  # Publish

        print("Post published successfully!")

    except Exception as e:
        print(f"Error publishing post: {e}")

def wordpress_login(username, password):
    try:
        # Initialize the WebDriver (replace with your browser's driver)
        driver = webdriver.Firefox()  # Or webdriver.Chrome(), etc.

        # Go to the WordPress.com login page
        driver.get("https://wordpress.com/log-in")

        # Wait for the username field to be visible and enter the username
        username_field = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, "usernameOrEmail"))
        )
        username_field.send_keys(username)

        # Continue button
        continue_button = driver.find_element(By.CSS_SELECTOR, "button[type='submit']")
        continue_button.click()

        # Wait for password field after continue button
        password_field = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, "password"))
        )
        password_field.send_keys(password)


        # Log in button
        log_in_button = driver.find_element(By.CSS_SELECTOR, "button[type='submit']")
        log_in_button.click()

        # Add a wait to ensure the login is complete (adjust as needed)
        time.sleep(5)  # You might need a more robust check here

        # Check for successful login (example, adjust based on your site's structure)
        # You might check for a specific element on the logged-in page
        try:
          check_button = driver.find_element(By.CSS_SELECTOR, "a.masterbar__item:nth-child(2) > svg:nth-child(1) > g:nth-child(1) > path:nth-child(1)")
          print("Login successful!")
        except:
            print("Login failed. Check credentials or website structure.")

        #list_my_sites(driver)

        create_and_publish_post(driver, "hello!", "world!!!")

    except Exception as e:
        print(f"An error occurred: {e}")

    finally:
        # Close the browser window
        # driver.quit()
        print("all done?!")

if __name__ == "__main__":
    username = "username"  # Replace with your username
    password = "password"  # Replace with your password
    website = "https://ashercmartin.wordpress.com"  # Replace with your wordpress website
    wordpress_login(username, password)
wordpress selenium-webdriver wordpress-gutenberg
1个回答
0
投票

我无法找到精确的 CSS 选择器,但我能够使用此代码发布帖子。一个可能的原因是 WORDPRESS 影子 DOM???

def create_and_publish_post(driver, title, content, tags=None, category=None, featured_image_path=None):
    
    def enter_text(element, text):  # Helper function for reliable text entry
        element.click()
        action = webdriver.ActionChains(driver)
        action.send_keys(text).perform()

    try:
        # 1. Navigate to "Add New Post"
        driver.get(website + "/wp-admin/post-new.php")  # Or your "Add New" URL
        time.sleep(2)

        #works! add a new block!
        #new_block = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.components-button > svg:nth-child(1)')))
        #new_block.click()
        action = webdriver.ActionChains(driver)
        action.send_keys(title + " " + content).perform()
        action.send_keys(Keys.ENTER).perform()
        action.send_keys("whats up!!!").perform()

        # 7. Publish button.components-button:nth-child(5)
        WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".editor-post-publish-panel__toggle"))).click()  # Open publish panel
        WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".editor-post-publish-button"))).click()  # Publish

        print("Post published successfully!")

    except Exception as e:
        print(f"Error publishing post: {e}")

感谢您的帮助!

;)

© www.soinside.com 2019 - 2024. All rights reserved.