Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Articles

create a simple automation script in Ruby,

create a simple automation script in Ruby, you can use the Watir or Selenium WebDriver libraries

To create a simple automation script in Ruby, you can use the Watir or Selenium WebDriver libraries, which allow you to automate interactions with web browsers. In this example, we'll use Watir to automate a web browser to perform a basic task: opening a website and searching for a term on a search engine.

First, you'll need to install the Watir gem. If you haven't installed it yet, use the following command:

```bash
gem install watir
```

Now, you can create a simple automation script in Ruby:

```ruby
require 'watir'

# Start a new browser instance
browser = Watir::Browser.new

# Navigate to a website (e.g., Google)
browser.goto('https://www.google.com')

# Find the search input field and enter a search term
search_input = browser.text_field(name: 'q')
search_term = 'Ruby programming'
search_input.set(search_term)

# Submit the search
search_input.send_keys(:enter)

# Wait for a few seconds (you can adjust the time as needed)
sleep 2

# Take a screenshot of the search results page
browser.screenshot.save('search_results.png')

# Close the browser
browser.close
```

In this script:

1. We require the 'watir' gem to use Watir for browser automation.

2. We create a new browser instance and navigate to the Google website.

3. We find the search input field and enter a search term (in this case, 'Ruby programming').

4. We submit the search by simulating the Enter key.

5. We wait for a few seconds (you can adjust the time as needed) to let the search results load.

6. We take a screenshot of the search results page and save it as 'search_results.png' in the current directory.

7. Finally, we close the browser.

You can modify this script to automate other web interactions, such as form submissions, button clicks, or data extraction. Watir and Selenium WebDriver are powerful tools for web automation and can be used for a wide range of tasks.

caa October 28 2023 123 reads 0 comments Print

0 comments

Leave a Comment

Please Login to Post a Comment.
  • No Comments have been Posted.

Sign In
Not a member yet? Click here to register.
Forgot Password?