0

After Login I am able to go this page, here is its HTML, JS & Python code. I want to make multiple choices and afterward, I want to save the choice using save button (input type="submit" onclick="saveVariations()") at the bottom of HTML.

    <body>        
    <tbody>

    <tr>
      <th scope="row">abc</th>
      <td>1</td>
      <td>1.0</td>
      <td><input type="checkbox" value="abc" onclick="addToVariations(this);"></td>
    </tr>

    <tr>
      <th scope="row">xyz</th>
      <td>1</td>
      <td>1.0</td>
      <td><input type="checkbox" value="xyz" onclick="addToVariations(this);"></td>
    </tr>
    .
    .
    .
    .
    </tbody>
  </table>
 <input type="button" value="Save variations" onclick="saveVariations()"/>
</body>

Here is the Python code; Giving error I am unable to even find the input tag to process the save button.

from bs4 import BeautifulSoup
import requests

class SessionLogin:
    def __init__(self, url_login, login, pwd):
        self.ses = requests.session()
        login_html = self.ses.get(url_login)
        soup_login = BeautifulSoup(login_html.content).find('form').find_all('input')
        my_dict = {}

    # override the inputs without login and pwd:
        my_dict['username'] = login
        my_dict['password'] = pwd
        self.ses.post(url_login,data=my_dict)
        print(type(soup_login))
    def get(self, URL):
        return self.ses.get(URL).text

url_login = "url"
session = SessionLogin(url_login, "username", "passwoord")



srcr = session.get("url")
print(BeautifulSoup(srcr).find('input'))

Following is the Javascript Code

  <script>
     var selectedVariations = [];
     function addToVariations(cb) {
         if (selectedVariations.indexOf(cb.value) > -1) {
             selectedVariations.splice(selectedVariations.indexOf(cb.value), 1);
         }
         else {
             selectedVariations.push(cb.value);
         }

         console.log(selectedVariations);
     }

function saveVariations() {
    if (selectedVariations.length > 0) {
        console.log(selectedVariations);
        $.ajax({
            type: "POST"
            , url: "/dishes/-K9uHK4M-pu9p9yJGl9a/variations"
            , data: JSON.stringify(selectedVariations)
            , contentType: 'application/json;charset=UTF-8'
            , success: function (result) {
                console.log(result.Status);
                if (result.Status == 200) {
                    $.notify("Succesfully saved variations!", "success");
                    window.location.href = "/";
                }
                else {
                    $.notify("Something went wrong!", "error");
                }

            }
        });
    }
}

Daniyal Tariq
  • 181
  • 1
  • 3
  • 12
  • I can't reproduce the problem. Your code works for me, but gives me the first ``, but I changed it to `find_all('input')[2]` and got ` ` – chickity china chinese chicken Jul 20 '17 at 00:43
  • Can you tell me how can I select any choice and save? – Daniyal Tariq Jul 20 '17 at 00:46
  • That is javascript - BeautifulSoup can't execute javascript. You may want to check out [selenium](http://selenium-python.readthedocs.io/), [PhantomJS](http://code.google.com/p/phantomjs/), or [pyv8](http://code.google.com/p/pyv8/). See this answer: [How can I simulate onclick event in python?](https://stackoverflow.com/a/38298498/1248974) – chickity china chinese chicken Jul 20 '17 at 00:52
  • selenium is slow, I tried on another project. And each time it opens the browsing and then shows output. Is there a way to do that without opening a browser with selenium. – Daniyal Tariq Jul 20 '17 at 00:59
  • Maybe not with selenium, but *maybe* [mechanize](http://wwwsearch.sourceforge.net/mechanize/) can do it, see this answer: [How to overcome Javascript “onclick” button to scrape web page?](https://stackoverflow.com/a/16426562/1248974). It was highly voted and accepted, so maybe it can. – chickity china chinese chicken Jul 20 '17 at 01:07
  • I need to do it with the requests, see the above JS code if you can help. – Daniyal Tariq Jul 20 '17 at 02:01
  • I will try, but sorry I'm not skilled in this, maybe @alecxe or someone more knowledgeable can offer ideas. – chickity china chinese chicken Jul 20 '17 at 17:40

0 Answers0