0

I'm running automated testing in Ruby.

I'm trying to write a script that finds id with display:none (visible only in mobile form), and clicks it.

Here is my code:

def open_menu
   page.find[:css, "a[id='mobile-nav-link']", visible: false].click
end

It gives me an error:

   Selenium::WebDriver::Error::InvalidSelectorError:
   invalid selector: No selector specified
3
  • Are you using Capybara? Are you sure square brackets are correct? A quick search reveals similar code using round brackets, not square. I.e., page.find(:css, "a[id='mobile-nav-link']", visible: false).click Commented Mar 11, 2017 at 21:17
  • Thank you! It was that simple! Commented Mar 11, 2017 at 21:56
  • Great, glad that helped! I've posted it as an answer. Commented Mar 12, 2017 at 4:13

2 Answers 2

0

A quick search reveals similar code using round brackets, not square. I.e.:

def open_menu
   page.find(:css, "a[id='mobile-nav-link']", visible: false).click
end
Sign up to request clarification or add additional context in comments.

Comments

0

Selenium only interacts with elements the way a user would.

If the link is only visible in mobile, I would suggest one of the following.

  1. View the page via the mobile URL.

  2. It's possible that you may be able to use Ruby's javascript execute to either update the control to be visible, or set the value that indicates you are viewing the page via a mobile device.

Updating the control to be visible

This link discusses how to set an element to visible using javascript execute: Selenium Webdriver - click on hidden elements

This link discusses how to use ruby's javascript execute: How to execute Javascript in Ruby written Webdriver test?

Put the two together, and you get something like this?

def open_menu
    elem = page.find[:css, "a[id='mobile-nav-link']", visible: false]
    js = "arguments[0].style.height='auto' arguments[0].style.visibility='visible';"
    driver.execute_script(js , elem)
    page.find[:css, "a[id='mobile-nav-link']", visible: true].click
end

I'm not entirely familiar with Ruby syntax, so I make no guarantees it will work copy/paste.

1 Comment

it fails on page.find .Gives me an error: 'Failure/Error: elem = page.find[:css, "a[id='mobile-nav-link']", visible: false] Selenium::WebDriver::Error::InvalidSelectorError: invalid selector: No selector specified'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.