0

Greetings fellow scrapers,

I'm trying to use Selenium in Python 3.4 to open a website's clunky interface and get the information I need from it. Sadly, since I need to submit a form (which directs to a different page without bringing up a new window) each time and there is no way to get all of the information in one go. The whole process goes:

"For" all relevant fields: # as of right now, this cannot be a for loop 
                           # because the elements are lost when I submit


  (1) click on the necessary buttons (make it visible) 
      in order to select a field in the form
  (2) submit the form
  (3) get the necessary information from the new page
  (4) go back to the first page

If possible, I would like to find a way of doing this that does not involve me clicking my way to a button each time- that is, I would like to eliminate step (1) of my loop and change step (4) to simply switching windows.

For clarification on how the form is submitted, there is a javascript function within the code that takes care of submission but the built-int Selenium method Element.submit() still works (so I can either .click() on the submit button or I can .submit() from any element in the form).

To make my question clearer, is there a way that the form output can be opened in a new window/tab, whether through inserting javascript or otherwise, so that I don't have to reload the first page and click its buttons so many times?

Please PM me if you would like more specific details about the code and the website, or tell me in an answer or comment if there is any crucial information missing from the question.

punyidea
  • 173
  • 1
  • 9

1 Answers1

0

So, I did some digging, and it turns out that it is possible to do this, in some cases.

I found this link about setting attributes, and after having read that forms can have attributes, especially target = _blank, which opens the form in a new (blank) window

This actually depends on how the form is submitted. If in the HTML the code has <form...id = someID, and the form element has some identifier (in this case, someID) you can manually change this attribute.

This opens forces the form to open in a new window the next time it is submitted:

Browser.execute_script('document.getElementById("someID").'setAttribute("target","_blank")')

Browser is the name I gave my WebDriver. This executes internal javaScript, which sets the target attribute of the form to _blank- just what I wanted.

Community
  • 1
  • 1
punyidea
  • 173
  • 1
  • 9