简体   繁体   English

如何使用 Selenium 将文本输入文本区域元素

[英]How to input text into text-area element with Selenium

recently I have begun to learn about web-scraping in Python.最近我开始在 Python 学习网络抓取。

I am currently attempting to make a script that can publish a blog post for me on the WordPress free blog platform.我目前正在尝试制作一个脚本,可以在 WordPress 免费博客平台上为我发布博客文章。

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.我还包含了许多 time.sleep() 命令来减少 web 浏览器的加载时间。

I have been able to login successfully and navigate to this page:我已经能够成功登录并导航到此页面:

https://i.imgur.com/Zm9HiTc.png 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 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):这是 html(第一个标题,然后是正文):

<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.到目前为止,我已经尝试使用完整的 xpath、相对 xpath、类名和 id。 I have also tried adding a.click() or.clear(), first, to select the element.我还尝试首先将 a.click() 或.clear() 添加到 select 元素。

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.我在其他一些答案中读到,您可以使用 avascript 更改为元素的文本,但是我不熟悉该语言,并且我的复制和粘贴尝试不起作用。

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); 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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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