简体   繁体   中英

How to input text into text-area element with Selenium

recently I have begun to learn about web-scraping in Python.

I am currently attempting to make a script that can publish a blog post for me on the WordPress free blog platform.

To do this I would input the Title and Body into the terminal and the scraper would run in headless mode, right now I still have it running it normal mode to troubleshoot. I have also included many time.sleep() commands to mitigate the loading times of the web browser.

I have been able to login successfully and navigate to this page:

https://i.imgur.com/Zm9HiTc.png

Once im here I click on the "write" button which takes me to this page:

https://i.imgur.com/qt1urQW.png

however, from here I am unable to get the scraper to input text in these two fields.

here is the html (first title, then body):

<textarea id="post-title-0" class="editor-post-title__input" placeholder="Add title" rows="1" style="overflow: hidden; overflow-wrap: break-word; resize: none; height: 85px;"></textarea>


<p aria-label="Empty block; start writing or type forward slash to choose a block" role="textbox" class="block-editor-block-list__block is-selected rich-text block-editor-rich-text__editable wp-block" aria-multiline="true" contenteditable="true" id="block-e71b147b-e967-4786-9b41-f59249702289" data-block="e71b147b-e967-4786-9b41-f59249702289" data-type="core/paragraph" data-title="Paragraph" tabindex="0" style="white-space: pre-wrap; transform-origin: center center;">&#65279;<span data-rich-text-placeholder="Start writing or type / to choose a block" contenteditable="false"></span></p>

so far i have tried to use to full xpath, the relative xpath, the classname, and the id. I have also tried adding a.click() or.clear(), first, to select the element.

I have read in some other answer that you can use avascript to change to text of an element, however I am unfamiliar with that language and my copy and paste attempt did notwork.

Anything helps, thank you in advance!

here is my a selected part of my code:







def wordpress_login():

    driver.get("https://wordpress.com/log-in?site=maw224651320.wordpress.com&redirect_to=%2Fhome%2Fmaw224651320.wordpress.com")
# first picture  

    Title = input ("Title: ") #input Title

    Body = input("Body: ")   #input body


    driver.get("https://wordpress.com/block-editor/post/maw224651320.wordpress.com")
    time.sleep(10)
#second picture

    driver.find_element_by_xpath("/html/body/div[1]/div[2]/div[1]/div[1]/div[3]/div[1]/div/div/div[1]/div/div[2]/div[1]/div[4]/div[2]/div/div/div[2]/div[2]/div[1]/div/textarea").send_keys(Title)
    time.sleep(5)



    driver.quit()

driver.find_element_by_id("post-title-0").send_keys('Title' + Keys.TAB ) driver.find_element_by_tag_name('p').send_keys(Body);

this way is working for me u try it it may work for u aswell

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time


url = 'https://example.com/wp-admin/post-new.php'


#make sure u insert ur chrom drive path following ur  pc path 

driver = webdriver.Chrome('ur path/chromedriver')
driver.get(url)

time.sleep(2)
driver.find_element_by_id('user_login').send_keys('insert ur username here')


driver.find_element_by_id('user_pass').send_keys('insert ur password here')


time.sleep(2)
driver.find_element_by_id('wp-submit').click()




driver.find_element_by_id("post-title-0").send_keys('L1' + Keys.TAB )
driver.find_element_by_tag_name('p').send_keys(content);



driver.find_element_by_name('Publish')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM