トップページ > Selenium API(逆引き) > 【Ruby】send_keys

【Ruby】send_keys・・・キーボード入力をする(通常キー)

【PR】業界トップクラスの「高単価報酬」「低マージン」を実現!レバテックフリーランス

「send_keys」を実行することで要素にキーボード入力することが出来ます。

要素.send_keys(string)の形で、引数に指定した文字列をテキストボックスやテキストエリアなどのHTML要素に入力することが出来ます。ここではElementクラスの「send_keys」を利用しますが、ダイアログにキーボード入力するAlertクラスの「send_keys」や、複数の処理を同時に実行するActionBuilderクラスの「send_keys」も存在します。いずれも同様に要素(テキストボックス、テキストエリア)に対して文字列を入力をします。

説明

◆メソッド
  ・send_keys(string)
◆使用形態
  ・element.send_keys('foo')
◆備考
  ・テキストボックスやテキストエリアなどのHTML要素で文字入力する為のメソッド
◆関連項目
  ・キーボード入力をする(特殊キー)
  ・要素に入力された値をクリアする

サンプル

require 'selenium-webdriver'
 
# Firefoxを起動
driver = Selenium::WebDriver.for(:firefox)
#指定したURLに遷移する
driver.get('https://www.google.co.jp')
#検索テキストボックスの要素をname属性名から取得
element = driver.find_element(:name, 'q')
#検索テキストボックスに文字列を入力
element.send_keys('キーボード入力をする(通常キー)')
※selenium version 4.1.0で動作確認をしています

実行結果

sendKeys2
 ※GoogleおよびGoogleロゴはGoogle Inc.の登録商標であり、同社の許可を得て使用しています。

Fatal error: Uncaught Google_Service_Exception: Error calling GET https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A138132118&start-date=7daysAgo&end-date=yesterday&metrics=ga%3Apageviews&dimensions=ga%3ApageTitle%2C+ga%3ApagePath&sort=-ga%3Apageviews&max-results=1000: (403) User does not have sufficient permissions for this profile. in /home/users/1/monda-muki/web/seleniumqref.com/google-api-php-client-1-master/src/Google/Http/REST.php:110 Stack trace: #0 /home/users/1/monda-muki/web/seleniumqref.com/google-api-php-client-1-master/src/Google/Http/REST.php(62): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request), Object(Google_Client)) #1 /home/users/1/monda-muki/web/seleniumqref.com/google-api-php-client-1-master/src/Google/Task/Runner.php(174): Google_Http_REST::doExecute(Object(Google_Client), Object(Google_Http_Request)) #2 /home/users/1/monda-muki/web/seleniumqref.com/google-api-php-client-1-master/src/Google/Http/REST.php(46): Google_Task_Runner->run() #3 /home/users/1/monda-muki/web/seleniumqref in /home/users/1/monda-muki/web/seleniumqref.com/google-api-php-client-1-master/src/Google/Http/REST.php on line 110