1

Working on my first ever Ruby on Rails project - a webscraper using Watir.

First things first, I am trying to log into a website, but Watir isn't putting in the usernames and passwords.

When it gets to the log in page, nothing gets typed. I've tried removing the username and password inputs just to see if it clicks the button and it still won't even click the button.

How can I get the text to go in the username/password fields and then have the button clicked?

I'm totally brand new to this sort of thing, so sorry if I'm not asking the question in an easily answerable way,

Here is what I have so far:

require 'watir'
require 'selenium-webdriver'


browser = Watir::Browser.new :safari #opens Safari
browser.window.maximize
browser.goto 'go.crmls.org'
browser.link(title: 'MLS Dashboard Login').click
browser.text_field(data_ph: 'User ID').set 'username'
browser.text_field(data_ph: 'Password').set 'password'
browser.button(id: 'loginbtn').click

Here is what I have in my gem file:

gem 'watir', '~> 6.16', '>= 6.16.5'
gem 'selenium-webdriver', '~> 3.6'

Everything is installed through Terminal and Bundle Install was used as well.

raa89
  • 25
  • 8

2 Answers2

0

Clicking on the element with MLS Dashboard Login opens a new tab. So you need to shift Selenium's focus to the newly opened TAB and find the elements as follows:

browser.windows.last.use
browser.element(xpath: "//div[@data-ph='User ID"]').set 'username'
browser.element(xpath: "//div[@data-ph='Password']").set 'password'

A bit canonically you can use:

browser.windows.last.use
browser.element(xpath: "//div[@class='form-control person' and @data-ph='User ID']').set 'username'
browser.element(xpath: "//div[@class='form-control lock' and @data-ph='Password']').set 'password'

Note: automatically waits for elements to exist, present and be enabled.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • You don't have to form the xpath in WATIR, if you write `browser.div`, it automatically forms the xpath and also another problem here is, he has to switch to the new window. – Rajagopalan Jul 03 '20 at 08:55
  • @Rajagopalan Good point, however this answerwas inspired from this [discussion](https://stackoverflow.com/questions/61521538/how-to-click-on-link-element-after-i-element-with-specific-text-using-watir) where you in your **accepted** answer suggested `browser.element(xpath: "//img[@src='/Images/Menu/arrow.gif']/..").click` Am I missing something here? – undetected Selenium Jul 03 '20 at 09:09
  • Yes, true, I should have used the following code `b.img(src: '/Images/Menu/arrow.gif').parent.click` But since I was presenting multiple option to click that same element, I was using that code. – Rajagopalan Jul 03 '20 at 10:32
0

There are few problems in your program, First of all, You don't have to require selenium-webdriver because watir is a wrapper around selenium-webdriver so when you are requiring watir, you are automatically requiring selenium-webdriver as well.Second off, the text_field you are setting the value is not the text_field but a div and also when you click the link MLS Dashboard Login It opens a new window so you have to switch to the new window before you enter the value. So write the following code, it would work.

browser.goto 'https://go.crmls.org/'
browser.link(text: 'MLS Dashboard Login').click
browser.windows.last.use
browser.div(data_ph: 'User ID').set 'username',:tab
browser.div(data_ph: 'Password').send_keys 'password'
browser.button(id: 'loginbtn').click
Rajagopalan
  • 5,465
  • 2
  • 11
  • 29
  • One question, if you are doing `:tab` possibly to focus on the next element, why do you `browser.div()`again for the _Password_ field? Won't you `send_keys 'password'` to the `active` element? – undetected Selenium Jul 03 '20 at 09:31
  • You are right with your assumption, b.div definitely focuses that element but the given website while I was trying to automate it, the text which is entered in the password field is getting lost after text was entered, so I just passed the tab key. If you try to with your Java binding or python binding, you will face the same problem. – Rajagopalan Jul 03 '20 at 10:33
  • 1
    It wasn't an assumption, plain logic :) – undetected Selenium Jul 03 '20 at 11:01
  • Yep, true! Selenium does that and you know that. So does WATIR because underhood watir uses selenium. – Rajagopalan Jul 03 '20 at 11:04
  • 1
    Thank you! This was easy to understand and it worked. For whatever reason, the Watir wasn't always going to the next tab, so I randomly used ```browser.windows.last.close``` AFTER ```browser.windows.last.use``` and it worked. Thanks! – raa89 Jul 03 '20 at 17:31