Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8326451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:45:56+00:00 2026-06-09T00:45:56+00:00

I am trying to click all links on stackoveflow horizontal menu (Questions, Tags, Users,

  • 0

I am trying to click all links on stackoveflow horizontal menu (Questions, Tags, Users, Badges, Unanswered). I have this code but this clicks on first link (this link is Questions), then prints 1, and after that raises error. What could be problem with this?

require 'watir-webdriver'

class Stackoverflow
  def click_all_nav_links
    b = Watir::Browser.new
    b.goto "http://stackoverflow.com"

    counter = 0
    b.div(:id => 'hmenus').div(:class => 'nav mainnavs').ul.lis.each do |li|
      li.a.click
      puts counter += 1
    end
  end
end

stackoverflow = Stackoverflow.new
stackoverflow.click_all_nav_links

Error message is:
https://gist.github.com/3242300

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:45:57+00:00Added an answer on June 9, 2026 at 12:45 am

    The StaleElementReferenceError often occurs when storing elements and then trying to access them after going to another page. In this case, the reference to the lis becomes stale after you click the links and navigate to a new page.

    You have to store off attributes or the index of the lis first. This will allow you to get a fresh reference to each li after clicking a link.

    Try this:

    class Stackoverflow
        def click_all_nav_links
            b = Watir::Browser.new
            b.goto "http://stackoverflow.com"
    
            #Store the text of each locate so that it can be located later
            tabs = b.div(:id => 'hmenus').div(:class => 'nav mainnavs').ul.lis.collect{ |x| x.text }
    
            #Iterate through the tabs, using a fresh reference each time
            tabs.each do |x|
                b.div(:id => 'hmenus').div(:class => 'nav mainnavs').ul.li(:text, x).a.click
            end
        end
    end
    
    stackoverflow = Stackoverflow.new
    stackoverflow.click_all_nav_links
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to click on all links on top menu on site http://watirwebdriver.com/
I am trying to get all the main links, then click on them and
I am trying to have a function done to all links on a page
i'm trying to add readonly to all the inputs and then on the click
Im trying to add click events to shapes within a visio file.. I have
I'm trying to get a mouse click event in Qt, this mouse click must
I have a bunch of menu links and want to change their style on
i am trying to select all links with a certain attribute, for some reason
I'm trying to track all links clicked with Google Analytics as events. So I
I am trying to loop through all the links on a site and autoclick

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.