I'm trying to login to a this website: https://advertising.flipkart.com/
I need to download and automate some reports as their console is not great but its been a while since I've used selenium and I'm drawing a blank. I'm trying to use the placeholder texts inside the form to identify the login fields but can't get it to work.
Here's the HTML of the form element where I've isolated the input fields for login, password, and submit button:
<form>
<div >
<div >
<div >
<div > Email address </div>
</div>
<div >
<div >
<input type="text"
placeholder="Enter email"
name=""
autocomplete="off"
value="USERNAME GOES HERE">
</div>
</div>
</div>
</div>
<div >
<div >
<div >
<div > Password </div>
</div>
<div >
<div >
<input type="password"
placeholder="Enter password"
name=""
autocomplete="off"
value="PASSWORD GOES HERE">
<div ><svg width="24px" height="24px"
viewBox="0 2 24 24" color="#363D41">
<g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g>
<g>
<path
d="M18.1660156,11.75 C18.1660156,11.8776048 18.0748707,12.1601541 17.8925781,12.5976562 C17.6464831,13.1901071 17.3639339,13.686847 17.0449219,14.0878906 C16.5162734,14.7350293 15.8828162,15.2454408 15.1445312,15.6191406 C14.2330684,16.0748721 13.1849018,16.3027344 12,16.3027344 C10.8150982,16.3027344 9.76693164,16.0748721 8.85546875,15.6191406 C8.10806918,15.2454408 7.47461197,14.7350293 6.95507812,14.0878906 C6.64518074,13.6959616 6.36718873,13.1992218 6.12109375,12.5976562 C5.92968654,12.1601541 5.83398438,11.8776048 5.83398438,11.75 C5.83398438,11.6223952 5.92968654,11.3398459 6.12109375,10.9023438 C6.36718873,10.3007782 6.64518074,9.80403842 6.95507812,9.41210938 C7.4837266,8.76497072 8.11262656,8.25455916 8.84179688,7.88085938 C9.75325977,7.42512793 10.8059836,7.19726562 12,7.19726562 C13.1940164,7.19726562 14.242183,7.42512793 15.1445312,7.88085938 C15.8919308,8.25455916 16.525388,8.76497072 17.0449219,9.41210938 C17.3639339,9.81315305 17.6419259,10.3098929 17.8789062,10.9023438 C18.0703135,11.3398459 18.1660156,11.6223952 18.1660156,11.75 Z M17.1542969,11.75 C16.9264312,11.0481736 16.6302102,10.4739606 16.265625,10.0273438 C15.8372374,9.48046602 15.3177114,9.05664213 14.7070312,8.75585938 C13.9414024,8.36393033 13.0390677,8.16796875 12,8.16796875 C10.9609323,8.16796875 10.0540403,8.36393033 9.27929688,8.75585938 C8.66861674,9.06575676 8.14909068,9.48958064 7.72070312,10.0273438 C7.35611797,10.4739606 7.05989697,11.0481736 6.83203125,11.75 C7.05989697,12.4518264 7.35611797,13.0260394 7.72070312,13.4726562 C8.14909068,14.0104194 8.66861674,14.4342432 9.27929688,14.7441406 C10.0540403,15.126955 10.9586537,15.3183594 11.9931641,15.3183594 C13.0276744,15.3183594 13.9322878,15.126955 14.7070312,14.7441406 C15.326826,14.4342432 15.8463521,14.0104194 16.265625,13.4726562 L16.2792969,13.4726562 C16.6347674,13.0260394 16.9264312,12.4518264 17.1542969,11.75 Z M13.6679688,10.0683594 C14.1328148,10.5240908 14.3652344,11.0800748 14.3652344,11.7363281 C14.3652344,12.3925814 14.1350934,12.9508441 13.6748047,13.4111328 C13.2145159,13.8714216 12.6562533,14.1015625 12,14.1015625 C11.3437467,14.1015625 10.7854841,13.8714216 10.3251953,13.4111328 C9.86490655,12.9508441 9.63476562,12.3925814 9.63476562,11.7363281 C9.63476562,11.0800748 9.86490655,10.5218122 10.3251953,10.0615234 C10.7854841,9.60123468 11.3437467,9.37109375 12,9.37109375 C12.6562533,9.37109375 13.2122373,9.6035133 13.6679688,10.0683594 Z M12.984375,10.7519531 C12.7109361,10.4785143 12.3828144,10.3417969 12,10.3417969 C11.6171856,10.3417969 11.2890639,10.4785143 11.015625,10.7519531 C10.7421861,11.025392 10.6054688,11.3535137 10.6054688,11.7363281 C10.6054688,12.1191425 10.7421861,12.4472643 11.015625,12.7207031 C11.2890639,12.994142 11.6171856,13.1308594 12,13.1308594 C12.3828144,13.1308594 12.7109361,12.994142 12.984375,12.7207031 C13.2578139,12.4472643 13.3945312,12.1191425 13.3945312,11.7363281 C13.3945312,11.3535137 13.2578139,11.025392 12.984375,10.7519531 Z"
fill="#363D41" fill-rule="nonzero"></path>
</g>
</g>
</g>
</svg></div>
</div>
</div>
</div>
<div ><a href="/forgot-password"
>
<div >Forgot Password?</div>
</a></div>
</div>
<div >
<div >
<button type="submit"
>
<div >Login</div>
</button>
</div>
<div >
<div > Do not have an account? </div>
<div ><a href="/register" >
<div >Register now</div>
</a></div>
</div>
</div>
</form>
This is the current code I have.
# import selenium module
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
# Login Information
url = "https://advertising.flipkart.com/"
login_id = "USERNAME"
login_password = "PASSWORD"
# Start the session
wd = webdriver.Chrome('./chromedriver')
def login(url, login_id, login_password):
""" This function will login to the website
parameters:
url: url of the website
login_id: login id of the user
login_password: password of the user
returns:
None
"""
# Open the website
wd.get(url)
# Find the input box with placeholder text "Enter email" and enter the login id
wd.find_element_by_xpath("//input[@placeholder='Enter email']").send_keys(login_id)
# Find the input box with placeholder text "Enter password" and enter the password
wd.find_element_by_xpath("//input[@placeholder='Enter password']").send_keys(login_password)
login(url, login_id, login_password)
CodePudding user response:
First, you are using deprecated syntax: find_element_by_*
was replaced with find_element(By.*...)
. The following is a correct way of logging into that website (you can adapt it to your own setup, just make sure you observe the imports, and the part after defining the browser/driver):
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument("window-size=1280,720")
webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)
url = 'https://advertising.flipkart.com/'
browser.get(url)
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input[placeholder="Enter email"]'))).send_keys('[email protected]')
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input[placeholder="Enter password"]'))).send_keys('password')
print('wrote credentials')
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[type="submit"]'))).click()
print('clicked submit button')
Selenium documentation can be found at https://www.selenium.dev/documentation/
CodePudding user response:
If you were to use inspect element, most browsers allow you to right click on the HTML element (the input fields in this case), and copy the XPath. So for example, for email it would be:
wd.find_element_by_xpath('//*[@id="app"]/div[1]/div[1]/div/div[2]/div/div[2]/form/div[1]/div/div[2]/div/input').send_keys(login_id)
This is not a perfect solution, but since you don't have an id or class, its one of the few solutions available. The biggest downside of this would be if the layout of the page was changed, at which point the XPath would change as well, meaning that you would have to update your script with the new XPath. However, if you only plan to use the script for yourself, this shouldn't be much of a problem.