Python Forum
How to upload images after first paragraph ends instead of uploading them at the end?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to upload images after first paragraph ends instead of uploading them at the end?
#1
In the below script, how do I upload images after the first paragraph ends instead of uploading it right at the very end.

I want the layout as

paragraph1

Images

Other paragraphs

Here is my script

from selenium.webdriver.common.keys import Keys
from selenium import webdriver
import os
from time import sleep
import docx2txt
browser = webdriver.Chrome('/usr/local/share/chromedriver')
username = 'admin'  #change it
password = 'password'
dir = 'Doc'
dir2 = 'Images'


all_files = os.listdir(dir+"/")

doc_files = list(filter(lambda x: x[-5:] == '.docx', all_files))


browser.get('http://localhost/testwebsite/wp-login.php')

print('\n-------------------------------')
print('Getting into the WordPress ...')
print('-------------------------------\n')


user_field = browser.find_element_by_xpath('//form//input[@type = "text"]')

user_field.click()

user_field.send_keys(username)

pass_field = browser.find_element_by_xpath('//form//input[@type = "password"]')
pass_field.click()

pass_field.send_keys(password)

submit = browser.find_element_by_xpath('//form//input[@type = "submit"]')
submit.click()

print('+---------------------------+')
print('|     Login Successfull!    |')
print('+---------------------------+\n\n')

sleep(2)

edit_post = browser.find_element_by_xpath('//div[@id = "wpwrap"]//ul[@id = "adminmenu"]//a[@class="wp-has-submenu wp-not-current-submenu menu-top menu-icon-post open-if-no-js menu-top-first"]')

edit_post.click()



for x in doc_files:
    print('+-----------------------------------+')
    print('|         Adding New Post           |')
    
    new_post = browser.find_element_by_xpath('//div[@class = "wrap"]//a[@class="page-title-action"]')
    new_post.click()
    
    title = browser.find_element_by_xpath('//div[@id = "titlewrap"]//input[@name = "post_title"]')
    title.click()
    post_title = os.path.splitext(x)[0]
    title.send_keys((post_title+ " Images").title())
    
    document = browser.find_element_by_xpath('//iframe[@id = "content_ifr"]')
    my_text = docx2txt.process("Doc/"+x)
    document.click()
    sleep(2)
    document.send_keys(my_text)
    
    
    insert_media = browser.find_element_by_xpath('//button[@id = "insert-media-button"]')
    insert_media.click()
    
    input_file = "//input[starts-with(@id,'html5_')]"
    
    
    image_files = os.listdir(dir2 + "/"  + post_title+"/")
    
    for image in image_files:
    	upload_button = browser.find_element_by_xpath(input_file).send_keys(os.getcwd() + "/" +dir2 + "/"+ post_title+"/"+ image)
  
    sleep(1)
    
    insert_images = browser.find_element_by_xpath('//button[@type = "button" and contains(text(),"Insert into post")]')
    
    sleep(4)
    
    insert_images.click()
    
    sleep(3)
    
    document.send_keys("\n")
    document.send_keys(Keys.CONTROL + Keys.END)
    document.send_keys("\n\n")
    
    sleep (5)
    submit = browser.find_element_by_xpath('//input[@type = "submit" and @value = "Publish"]')
    
    browser.execute_script("arguments[0].click();", submit)
    
    print('|      Post Added Successfully!     |')
    print('+-----------------------------------+\n\n')
    
    sleep(2)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Uploading images from multipart/data/-from get corrupted jdc18 0 1,936 Sep-30-2020, 07:16 PM
Last Post: jdc18
  Uploading multiple pdfs to Flask ( Sql alchmey) with additional variables KirkmanJ 10 9,485 Jul-27-2018, 09:49 AM
Last Post: KirkmanJ
  Beautiful Soup - Title + Paragraph into a text file dj99 4 6,427 Jul-14-2018, 01:37 PM
Last Post: dj99
  [Flask] Uploading CSV file to flask, only first line being uploaded. Help ! KirkmanJ 2 6,791 Jun-25-2018, 02:24 PM
Last Post: KirkmanJ

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020