2

This is the form from https://contacts.google.com/ when adding a new contact

enter image description here

I have tried to locate them by xpath, by class name , by id , by label but not able to locate . Below is the example trying to locate by xpath

         browser.text_field(xpath: "//*[@id='c1']/div[2]/div[1]/div/div[1]/div/div[1]").set "#{firstname}"
         sleep (5)
         browser.text_field(xpath: "//*[@id='c1']/div[2]/div[3]/div/div[1]/div/div[1]/input").set "#{lastname}"
         sleep (5)
         browser.text_field(xpath: "//*[@id='c4']/div/div[1]/div/div[1]/div/div[1]/input").set "#{company}"
         sleep (5)
         browser.text_field(xpath: "//*[@id='c4']/div/div[2]/div/div[1]/div/div[1]/input").set "#{job}"
         sleep (5)
         browser.text_field(xpath: "//*[@id='c6']/div[1]/div[2]/div[1]/div/div[1]/input").set "#{number}"
         sleep (5)
         browser.text_field(xpath: "//*[@id='c15']/div/div[1]/div[2]/textarea").set "#{notes}"
         sleep (5)
         browser.div(xpath: "//*[@id='ow44']/div[3]/div/button[2]/div").wait_until(&:present?).click
Astrit Shuli
  • 619
  • 4
  • 20

1 Answers1

2

The HTML of the input fields looks like:

<input type="text" class="whsOnd zHQkBf" jsname="YPqjbf" autocomplete="off" tabindex="0" aria-label="First name" autofocus="" data-initial-value="bbbb" badinput="false" dir="ltr">

Note that there is an aria-label attribute that provides a good description of the field, which makes it a good locator.

Try:

browser.text_field(aria_label: 'First name').set "#{firstname}"

Similar can be done with the other fields.

Justin Ko
  • 46,526
  • 5
  • 91
  • 101
  • Just tried it doesnt work :( ----- waiting for #"First Name", :tag_name=>"input"}> to be located; Maybe look in an iframe? (Watir::Exception::UnknownObjectException) – Astrit Shuli Jan 29 '20 at 14:54
  • 1
    There was a typo in the attribute value. It should be "First name" not "First Name". Sorry, not able to actually test this code as Google doesn't seem to allow me to log in when automated via Watir. – Justin Ko Jan 29 '20 at 15:09
  • wow thank you i really didn't catch that . it all worked , if you want i can tell you how to skip the login part for automation purposes :) – Astrit Shuli Jan 29 '20 at 15:21
  • I'd be curious to know how you do that. – Justin Ko Jan 29 '20 at 15:31
  • Its kind of a trick please follow this topic : https://stackoverflow.com/a/40622750/10658246 , You need to open a new google account when you are in webdriver on program running , and after you created a new account the next time you execute the program again it logs you in automatically . – Astrit Shuli Jan 29 '20 at 15:39
  • Well that's interesting. Useful to know. Thanks. – Justin Ko Jan 29 '20 at 16:01