Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to fill in fields that aren't inside a form tag and click on elements that aren't links? #591

Closed
randall-coding opened this issue Feb 17, 2022 · 3 comments

Comments

@randall-coding
Copy link

randall-coding commented Feb 17, 2022

From reading the docs, it seems that in order to fill in the value of an input field that field must be inside a form tag and to click on an element that element needs to be a link.

In my current use case I need to break both of those rules, the input field is not in a form and the element to click on is not a link. Any help is greatly appreciated.

@flavorjones
Copy link
Member

Hi @randall-coding, thanks for asking this question. I'll try to help.

Can you provide an HTML snippet that demonstrates the form and fields that you're operating on? I've found that grounding the discussion in a concrete example is usually very helpful.

I'll take some guesses at what you're working with ...

input field not inside a form tag

Without more information, I suspect what you're seeing is an input field that is not contained within the form tag, but is using the form attribute to declare its association with that form. Does that sound right?

If that's right, then the underlying issue is that Mechanize hasn't been kept up-to-date with the HTML5 spec (which has been constantly changing over the past 10+ years). If you peek at lib/mechanize/form.rb you'll see the parse method only searches for input fields within the form's DOM subtree and not elsewhere in the document as described in the form attribute docs I hyperlinked above.

click on an element that's not a link

This one is a bit stranger -- I would like to understand your specific use case more deeply. You can try to do this by using Mechanize#click which tries to do the right thing (if the element has an href or src attribute it will follow that URI).

But it seems more likely that the page you're working with relies on Javascript to make an element clickable in the browser, and/or traps the click event to trigger a Javascript callback. Mechanize isn't a full browser, and it doesn't attempt to do anything with Javascript that a browser might normally load. If this is what you're dealing with, you may want to use a tool that will leverage actual browser functionality, like capybara which uses Selenium and something like Chromedriver to load the page in a headless browser. Capybara is more complicated to use, and slower than Mechanize, but in return you get 100% of browser functionality.

Let me know if anything here is helpful, or if I've made incorrect assumptions.

@flavorjones
Copy link
Member

I've created #592 to track missing HTML5 functionality.

@randall-coding
Copy link
Author

@flavorjones Thanks for the response. Yes it turns out the page is using javascript and in fact angular. I ended up switching over to Selenium.

@sparklemotion sparklemotion locked and limited conversation to collaborators May 20, 2022
@flavorjones flavorjones converted this issue into discussion #598 May 20, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants