Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to send text via sendkeys
#1
Hi. This is a script to add image and text to wordpress. The adding images part works alright but I am unable to send the text via document.send_keys(my_text) because I get the selenium.common.exceptions.WebDriverException: Message: unknown error: keys should be a string error. I have tried downgrading selenium as well. I also did a print(my_text) and it printed the output text correctly so I know its getting picked correctly but not being sent.

Error:
Traceback (most recent call last): File "script.py", line 88, in <module> document.send_keys(my_text) File "/home/sam/Dev/Python/env/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 347, in send_keys self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': keys_to_typing(value)}) File "/home/sam/Dev/Python/env/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 491, in _execute return self._parent.execute(command, params) File "/home/sam/Dev/Python/env/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 238, in execute self.error_handler.check_response(response) File "/home/sam/Dev/Python/env/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 193, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: keys should be a string (Session info: chrome=69.0.3497.81) (Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 4.15.0-33-generic x86_64)
from selenium.webdriver.common.keys import Keys
import os
from time import sleep
import textract
from selenium import webdriver
browser = webdriver.Chrome('/usr/local/share/chromedriver')
print()

username = 'admin'  #change it
password = 'password'
dir = 'Doc'
dir2 = 'Images'


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

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


browser.get('http://localhost/website/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"))

    document = browser.find_element_by_xpath('//iframe[@id = "content_ifr"]')


    my_text = textract.process(dir+'/'+x)
    document.click()

    sleep(1)

    document.send_keys(my_text)
    
    insert_media = browser.find_element_by_xpath('//button[@id = "insert-media-button"]')
    insert_media.send_keys("\n")
    
    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(2)
    
    insert_images.click()
    
    sleep(1)
    
    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
#2
Bump!
Reply


Forum Jump:

User Panel Messages

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