Sakib
Sakib

Reputation: 85

Is it possible to get element by class in selenium webdriver ruby?

I have a class like this:

<div class="qa-share-message ng-isolate-scope ng-valid ta-root
ng-dirty focussed" ng-model="message" text-angular="">

I want to type in this field. i have tried this:

driver.find_elements(:class, "qa-share-message").send_keys("This is a test message")

After running the code it shows error:

undefined method `send_keys' for [#<Selenium::WebDriver::Element:0x65990040 id="33">]:Array

Is it possible to get element by class?

Upvotes: 1

Views: 2834

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

You obviously got elements by class. Look:

undefined method `send_keys' for [# Selenium::WebDriver::Element:0x65990040 id="33"]:Array

the error above clearly says: the call to find_elements succeeded and returned an Array instance, containing Elements. Everything you now need is to either send_keys to each found element:

driver.find_elements(:class, "qa-share-message").each do |e|
  e.send_keys("This is a test message")
end

or to send it to one of them, e.g. to the very first one:

driver.find_elements(:class, "qa-share-message")
      .first
      .send_keys("This is a test message")

Upvotes: 1

Related Questions