質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

selenium

Selenium(セレニウム)は、ブラウザをプログラムで作動させるフレームワークです。この原理を使うことにより、ブラウザのユーザーテストなどを自動化にすることができます。

Q&A

解決済

2回答

2587閲覧

python wordpressに自動入力

Mario_11

総合スコア95

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

selenium

Selenium(セレニウム)は、ブラウザをプログラムで作動させるフレームワークです。この原理を使うことにより、ブラウザのユーザーテストなどを自動化にすることができます。

0グッド

0クリップ

投稿2019/09/08 13:50

編集2019/09/09 12:14

下記のサイトのようにワードプレスに自動で入力してみたいと思ったのですが、タイトルは入るのですが、本文には入りませんでした。
一年以上前の投稿なので何か仕様がかわっていたりしているのでしょうか?

https://qiita.com/kkdmgs110/items/f0e3c17a64736d18fbc5

python

1title = "Test" 2content = "test post" 3driver.find_element_by_css_selector("#title").send_keys(title) 4driver.find_element_by_css_selector("#content_ifr").send_keys(Content) 5driver.find_element_by_css_selector("#save-post").click 6time.sleep(5) #waiting for post

よろしくお願いいたします。

イメージ説明
イメージ説明

html

1<iframe id="content_ifr" frameborder="0" allowtransparency="true" title="リッチテキストエリア。Control-Option-H キーでヘルプを表示できます。" style="width: 100%; height: 300px; display: block;"></iframe>
<html lang="ja" style="overflow-y: hidden;"><head><style id="mceDefaultStyles" type="text/css">.mce-content-body div.mce-resizehandle {position: absolute;border: 1px solid black;box-sizing: content-box;background: #FFF;width: 7px;height: 7px;z-index: 10000}.mce-content-body .mce-resizehandle:hover {background: #000}.mce-content-body img[data-mce-selected],.mce-content-body hr[data-mce-selected] {outline: 1px solid black;resize: none}.mce-content-body .mce-clonedresizable {position: absolute;outline: 1px dashed black;opacity: .5;filter: alpha(opacity=50);z-index: 10000}.mce-content-body .mce-resize-helper {background: #555;background: rgba(0,0,0,0.75);border-radius: 3px;border: 1px;color: white;display: none;font-family: sans-serif;font-size: 12px;white-space: nowrap;line-height: 14px;margin: 5px 10px;padding: 5px;position: absolute;z-index: 10001} .mce-visual-caret {position: absolute;background-color: black;background-color: currentcolor;}.mce-visual-caret-hidden {display: none;}*[data-mce-caret] {position: absolute;left: -1000px;right: auto;top: 0;margin: 0;padding: 0;} .mce-content-body .mce-offscreen-selection {position: absolute;left: -9999999999px;max-width: 1000000px;}.mce-content-body *[contentEditable=false] {cursor: default;}.mce-content-body *[contentEditable=true] {cursor: text;} </style><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><link rel="stylesheet" type="text/css" id="u0" href="https://llcmujin.com/wp-includes/js/tinymce/skins/lightgray/content.min.css?wp-mce-4940-20190515"><link rel="stylesheet" type="text/css" id="u1" href="https://llcmujin.com/wp-includes/css/dashicons.min.css?ver=5.2.3&amp;wp-mce-4940-20190515"><link rel="stylesheet" type="text/css" id="u2" href="https://llcmujin.com/wp-includes/js/tinymce/skins/wordpress/wp-content.css?ver=5.2.3&amp;wp-mce-4940-20190515"><link rel="stylesheet" type="text/css" id="u3" href="https://llcmujin.com/wp-content/plugins/search-everything/static/css/se-styles.css?wp-mce-4940-20190515"></head><body id="tinymce" class="mce-content-body content post-type-post post-status-draft post-format-standard page-template-default locale-ja mceContentBody webkit wp-editor wp-autoresize html5-captions" data-id="content" contenteditable="true" style="overflow-y: hidden;"><p><br data-mce-bogus="1"></p></body></html>

コードに書くと横長になってしまったので直接書きました。

html

1<html lang="ja" style="overflow-y: hidden;"><head><style id="mceDefaultStyles" type="text/css">.mce-content-body div.mce-resizehandle {position: absolute;border: 1px solid black;box-sizing: content-box;background: #FFF;width: 7px;height: 7px;z-index: 10000}.mce-content-body .mce-resizehandle:hover {background: #000}.mce-content-body img[data-mce-selected],.mce-content-body hr[data-mce-selected] {outline: 1px solid black;resize: none}.mce-content-body .mce-clonedresizable {position: absolute;outline: 1px dashed black;opacity: .5;filter: alpha(opacity=50);z-index: 10000}.mce-content-body .mce-resize-helper {background: #555;background: rgba(0,0,0,0.75);border-radius: 3px;border: 1px;color: white;display: none;font-family: sans-serif;font-size: 12px;white-space: nowrap;line-height: 14px;margin: 5px 10px;padding: 5px;position: absolute;z-index: 10001} 2.mce-visual-caret {position: absolute;background-color: black;background-color: currentcolor;}.mce-visual-caret-hidden {display: none;}*[data-mce-caret] {position: absolute;left: -1000px;right: auto;top: 0;margin: 0;padding: 0;} 3.mce-content-body .mce-offscreen-selection {position: absolute;left: -9999999999px;max-width: 1000000px;}.mce-content-body *[contentEditable=false] {cursor: default;}.mce-content-body *[contentEditable=true] {cursor: text;} 4</style><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><link rel="stylesheet" type="text/css" id="u0" href="https://llcmujin.com/wp-includes/js/tinymce/skins/lightgray/content.min.css?wp-mce-4940-20190515"><link rel="stylesheet" type="text/css" id="u1" href="https://llcmujin.com/wp-includes/css/dashicons.min.css?ver=5.2.3&amp;wp-mce-4940-20190515"><link rel="stylesheet" type="text/css" id="u2" href="https://llcmujin.com/wp-includes/js/tinymce/skins/wordpress/wp-content.css?ver=5.2.3&amp;wp-mce-4940-20190515"><link rel="stylesheet" type="text/css" id="u3" href="https://llcmujin.com/wp-content/plugins/search-everything/static/css/se-styles.css?wp-mce-4940-20190515"></head><body id="tinymce" class="mce-content-body content post-type-post post-status-draft post-format-standard page-template-default locale-ja mceContentBody webkit wp-editor wp-autoresize html5-captions" data-id="content" contenteditable="true" style="overflow-y: hidden;"><p><br data-mce-bogus="1"></p></body></html>

追記
イメージ説明

html

1<textarea class="wp-editor-area" style="height: 360px; margin-top: 67px;" autocomplete="off" cols="40" name="content" id="content" aria-hidden="false"></textarea>

イメージ説明

html

1<button type="button" id="content-html" class="wp-switch-editor switch-html" data-wp-editor-id="content">テキスト</button>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

shirai

2019/09/08 15:49

下書きを書く画面のformタグで囲まれた部分のHTMLを全て貼り付けてもらえませんか。
Mario_11

2019/09/08 22:48 編集

回答ありがとうございます。 formタグで囲まれたHTML部分が凄く長く入りきらなかったので下書きを書く場所のHTMLを載せました。 <iframe id="content_ifr" frameborder="0" allowtransparency="true" title="リッチテキストエリア。Control-Option-H キーでヘルプを表示できます。" style="width: 100%; height: 300px; display: block;"></iframe> ちなみにselectorも #content_ifr です。 よろしくお願いいたします。
guest

回答2

0

ベストアンサー

エディタだけiframeで区切られていますね。
他の部分のDOMまで見ないと断定はできないですが。

python

1Content = 'test post' 2driver.switch_to.frame(driver.find_element_by_tag_name('iframe')) 3driver.find_element_by_css_selector("#content_ifr").send_keys(Content.replace('¥r','¥¥r').replace('¥n','¥¥n') 4driver.switch_to.default_content()

ご参考までにこちらを。

返信を受けて追記

一般的にseleniumを使うとリッチテキストエディタに直打ちするのは難しいです。
標準入力モードに変えてやって、入力するというのが王道です。

ですので、色々なhtmlタグを入れたかったら、そもそも変数Content自体を、
'<p>test post</p>'などとhtmlタグで装飾済みの値にしてやる必要があります。

プログラムは以下の通りです。

python

1title = "Test" 2content = "test post" 3driver.find_element_by_css_selector("#title").send_keys(title) 4 5# ユーザの体感速度向上のため、エディタだけ後からajaxで読み込むパターンがあり、 6# ajax終了前にDOMを見るとNoSachElementExceptionを起こす可能性があるため数秒待つ 7time.sleep(2) 8 9# テキストモードに移行 10driver.find_element_by_link_text('テキスト').click() 11 12# 文字を入力 13driver.find_element_by_id('content').send_keys(Content.replace('¥r','¥¥r').replace('¥n','¥¥n')) 14 15driver.find_element_by_css_selector("#save-post").click

投稿2019/09/09 09:23

編集2019/09/09 12:28
shirai

総合スコア1289

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Mario_11

2019/09/09 11:11

回答ありがとうございます。 エラーが出てしまいました。 Traceback (most recent call last): File "ABC.py", line 38, in <module> driver.find_element_by_css_selector("#content_ifr").send_keys(Content.replace('¥r','¥¥r').replace('¥n','¥¥n')) File "/Users/.../Desktop/New_python/selenium/webdriver/remote/webdriver.py", line 598, in find_element_by_css_selector return self.find_element(by=By.CSS_SELECTOR, value=css_selector) File "/Users/.../Desktop/New_python/selenium/webdriver/remote/webdriver.py", line 978, in find_element 'value': value})['value'] File "/Users/.../Desktop/New_python/selenium/webdriver/remote/webdriver.py", line 321, in execute self.error_handler.check_response(response) File "/Users/.../Desktop/New_python/selenium/webdriver/remote/errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"#content_ifr"} (Session info: headless chrome=78.0.3899.0) DOMについて調べたのですが他の部分のDOMがどれかわかりませんでしたが、開発者ツールで同じ場所を示しているコードがあったので載せます。 <body id="tinymce" class="mce-content-body content post-type-post post-status-draft post-format-standard page-template-default locale-ja mceContentBody webkit wp-editor wp-autoresize html5-captions" data-id="content" contenteditable="true" style="overflow-y: hidden;"><p><br data-mce-bogus="1"></p></body> ちなみに frameタグは1つしかないと思います。 もし他に情報が必要であれば追記します。 よろしくお願いします。
shirai

2019/09/09 11:24

そしたらframeタグ配下のDOM全てと実際にGUIでそのエディタがどう表示されているかを画像で見せていただけますか。
Mario_11

2019/09/09 11:46

追記致しました。間違っていたら修正いたします。
shirai

2019/09/09 12:03

全部把握できました。 ですが、回答するには後2つ情報が必要です。 1. 「ビジュアル」「テキスト」と2つタブがありますが、「テキスト」のDOMを下さい。「ここをクリックしたらテキストモードに変化する」という所にマウスを持ってきて、右クリックで開発者ツールを開き、青色でハイライトされている部分のDOMです。 2. GUIでテキストモードを選択(「テキスト」をクリック)した後の、frameタブ配下のDOMを下さい。もしframeタブがなくなっていたら、「テキストモードのままで文字を入力するところ」でクリックし、文字を入力できるモードにした後、マウスそのままの場所で右クリックをし、開発者ツールを開き、青色にハイライトされている部分のDOMを下さい。
Mario_11

2019/09/09 12:14

詳しく説明していただきありがとうございます。 追記致しました。よろしくお願いいたします。
shirai

2019/09/09 12:27

回答追記しました。エラーが出たら言ってください。
Mario_11

2019/09/09 12:40

ありがとうございます。 driver.find_element_by_link_text('テキスト').click()の部分にエラーが出てしまいました。 selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"テキスト"} (Session info: headless chrome=78.0.3899.0) ですが、 driver.find_element_by_xpath("/html/body/div[1]/div[2]/div[2]/div[1]/div[3]/form/div/div/div[1]/div[2]/div/div[1]/div[2]/button[2]").click() とxpathに変更してみたところなぜかうまくいき投稿を見ると入力されていたのですが、これはこれでOKなのでしょうか?
shirai

2019/09/09 13:22

だとしたら他にもテキストという名前のDOMがどこかにあったんでしょうね・・・。 もちろん動けばOKですよ!
Mamiya_tom

2019/09/09 14:22

なるほど、次はDOMについて詳しく勉強していきます。 漠然な質問に対して、分かりやすく教えてくださり本当にありがとうございました。助かりました! また何かあればよろしくお願いします!
guest

0

質問に記載されている Qiita の記事に 参考にした URL ( https://review-of-my-life.blogspot.com/2017/11/python-wordpress.html )が書かれています。その URL を見ると WordPress の画面キャプチャがありますが、WordPress 4.9までに使われていた TinyMCE エディタの画面ですね。


対象となる WordPress のバージョンはいくつでしょうか?

WordPress 4.9.10 以前か Classic Editor プラグインをインストールした環境で使われる TinyMCE エディタには、#content_ifr は存在していますが、WordPress 5.0 以降で採用された新しいエディタ Gutenberg には、#content_ifr は、存在しません。

簡単に対処するには、 WordPress に Classic Editor プラグインをインストールしてください。

投稿2019/09/08 21:39

CHERRY

総合スコア25171

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Mario_11

2019/09/08 22:34

回答ありがとうございます。 バージョンはWordPress 5.2.3でClassic Editorもプラグインしているのですが、 なぜかテキストが入ってくれません。仮に("テスト")とそのまま入力しても入らないので、ワードプレスに問題があるのでしょうか?
CHERRY

2019/09/09 00:58

なるほど、Clasic Editor 環境ですか。 ちょっと多忙なので、時間ができたら環境を用意して試してみますね。
退会済みユーザー

退会済みユーザー

2019/09/09 01:08

横からすみません。 これ、ブラウザの自動操作必須なんですか? 要件次第ではもっと効率の良い(管理しやすい)方法がある気がするのですが。
Mario_11

2019/09/09 02:09

よろしくお願いいたします。 必須というわけではないのですが、コピペや手入力より早いと思ったので自動操作にしています。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問