【问题标题】:How to input text into text-area element with Selenium如何使用 Selenium 将文本输入到文本区域元素中
【发布时间】:2020-06-15 03:37:50
【问题描述】:

最近我开始学习 Python 中的网络抓取。

我目前正在尝试制作一个脚本,可以在 WordPress 免费博客平台上为我发布博客文章。

为此,我将标题和正文输入终端,刮板将在无头模式下运行,现在我仍然让它在正常模式下运行以进行故障排除。我还包含了许多 time.sleep() 命令来减少 Web 浏览器的加载时间。

我已经能够成功登录并导航到此页面:

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

在这里,我单击“写入”按钮,将我带到此页面:

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

但是,从这里我无法让刮板在这两个字段中输入文本。

这里是 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>

到目前为止,我已经尝试使用完整的 xpath、相对 xpath、类名和 id。 我也尝试过先添加一个 .click() 或 .clear() 来选择元素。

我已经阅读了其他一些答案,您可以使用 avascript 更改为元素的文本,但是我不熟悉该语言,并且我的复制和粘贴尝试不起作用。

任何帮助,提前谢谢你!

这是我选择的代码部分:







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()

【问题讨论】:

标签: python python-3.x selenium web-scraping


【解决方案1】:

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

【讨论】:

  • 您能否提供一些上下文来理解您的解决方案? How to Answer
  • 这对我不起作用,selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":" [id="post-title-0"]"}
【解决方案2】:

这种方式对我有用,你试试,它也可能对你有用

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    • 2021-07-03
    • 2015-08-12
    • 2021-06-27
    • 1970-01-01
    • 2014-01-27
    相关资源
    最近更新 更多