'Python click button with requests
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.
Solution 1:[1]
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.
Solution 2:[2]
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.
Solution 3:[3]
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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Community |
Solution 2 | enninet |
Solution 3 | gamerfan82 |