0

I have been trying to 'click' a button on a site with the requests module on a website, but I can't get it working.

This is the button on the site:

<div data-expected-currency="1" data-asset-type="T-Shirt" class="btn-primary btn-medium PurchaseButton " data-se="item-buyforrobux" data-item-name="T-Shirt" data-item-id="307054354" data-expected-price="2" data-product-id="28239339" data-expected-seller-id="96294876" data-bc-requirement="0" data-seller-name="All Player Clothing">
                                         Buy with R$
                                    </div>

This is what I've been trying to do in Python:

urlattempt = 'https://www.roblox.com/NewLogin'
    values = {'Username': 'USER',
      'Password': 'PASSWORD',
              'action': 'login'}
    rpost = requests.post(urlattempt, data=values)
    cookies = rpost.cookies
    values2 = {'action': 'btn-primary btn-medium PurchaseButton'}
    rpost2 = requests.post('https://www.roblox.com/item.aspx?id='+price2, cookies=cookies, data=values2)

The login part works fine, it's just the last part (from values2) that doesn't works.

hellobro
  • 21
  • 1
  • 2
  • 5

3 Answers3

2

Clicking a button is not directly possible with the requests library. If you really need to click on something from your python code, you need a scriptable, possibly headless browser.

What you can do, is to figure out what request goes to the server when the button is clicked and try to recreate it using requests. Use your browsers developer tools to inspect what requests are made and what contents they have. I have answered a similar question here.

Community
  • 1
  • 1
Dave
  • 1,784
  • 2
  • 23
  • 35
2

It is better to use the selenium library

from selenium import webdriver
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(executable_path='chromedriver.exe',options=options)
driver.get(url)
driver.find_element_by_xpath("xpath").click()
gamerfan82
  • 33
  • 5
1

You can try session, so that it will carry cookies when you change to another page in the same site. Here is how it looks like:

import requests

s=requests.session()

url_login=''
url_data=''

payload={'**strong text**':'usrname','Password':'pswd'}
request1=s.post(url_login,data=payload) # this is to pass the 

request2=s.get(url_data)

As for clicking a button, you may need to find out the action behind the button. There might be a handler against an element Id.

enninet
  • 11
  • 2