I have been trying to iterate through a website book listing using python. The website is https://www.vet-ebooks.com/. Issue is i need to perform a login before download the book. but login using the python is not working. Initial code that i have written is:
import requests
# Start a session
session = requests.Session()
# Define the login data
login_payload = {
'log': 'test',
'pwd': 'test',
'rememberme': 'forever',
'ihcaction':'login',
'ihc_login_nonce': '52af5a580a'
}
# Send a POST request to the login page
login_req = session.post('https://www.vet-ebooks.com/user-login/', data=login_payload)
# Check if login was successful
if login_req.status_code == 200:
print("Login successful")
else:
print("Login failed")
However, it shows that the Login was successful but it does not worked. I had switched my approach to Selenium.
# Selenium 4
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.set_capability('browserless:token', 'TOKENHERE')
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--headless")
driver = webdriver.Remote(
command_executor='https://chrome.browserless.io/webdriver',
options=chrome_options
)
driver.get("https://www.vet-ebooks.com/user-login/")
# replace with your username and password
username = "testusername"
password = "testpasshere"
wait = WebDriverWait(driver, 10)
# again, get the login page and set the cookies
driver.get('https://www.vet-ebooks.com/user-login/')
# find username field and enter username
username_field = wait.until(EC.presence_of_element_located((By.NAME, "log")))
username_field.clear()
username_field.send_keys(username)
# find password field and enter password
password_field = wait.until(EC.presence_of_element_located((By.NAME, "pwd")))
password_field.clear()
password_field.send_keys(password)
# find login button and click it
login_button = wait.until(EC.presence_of_element_located((By.NAME, "Submit")))
login_button.click()
# Print the redirected URL
redirected_url = driver.current_url
print("Redirected URL:", redirected_url)
driver.quit()
But this code does not work either, redirect_url remains the same at /user-login/ please can anyone suggest me how this issue can be resolved?