Sep-15-2018, 06:53 AM
(This post was last modified: Sep-15-2018, 06:54 AM by SamLearnsPython.)
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
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)