nmahesh
nmahesh

Reputation: 43

How to send inputs to an element using selenium if there is not id or class name?

I have this user input field (for a username) from this webpage (https://robinhood.com/crypto/BTC) that I have no idea how to access using Python Selenium.

I tried using Xpath and class name but was not able to get it to work.

<div class="form-group touched"><label><div>Email</div><div><input autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" required="" name="username" type="text" value=""></div></label></div>

Any suggestions on how to get that element? I want to send_key and input a username in that field. I tried this but I get a timeout error when I run and I'm not even sure if that's the correct way of selecting it:

username = WebDriverWait(driver, 10).until(EC.element_to_be_clickable(
    (By.XPATH, "//input[contains(text(),'username')]")))

username.send_keys("username")

Upvotes: 1

Views: 141

Answers (1)

0m3r
0m3r

Reputation: 12499

Work with css_selector

Here is full example

import time

from selenium import webdriver
from selenium.webdriver.common.keys import Keys


class RobinHoodBot:
    def __init__(self, email, password):
        self.chrome_browser = webdriver.Chrome()
        self.email = email
        self.password = password

    def login(self):
        self.chrome_browser.get("https://robinhood.com/crypto/BTC")
        time.sleep(2)

        login = self.chrome_browser.find_element_by_link_text("Log In")
        login.click()
        time.sleep(2)

        email_box = 'div.form-group:nth-child(1) > label:nth-child(1) > div:nth-child(2) > input:nth-child(1)'
        email_input = self.chrome_browser.find_element_by_css_selector(email_box)

        password_box = 'div.form-group:nth-child(2) > label:nth-child(1) > div:nth-child(2) > input:nth-child(1)'
        password_input = self.chrome_browser.find_element_by_css_selector(password_box)

        email_input.send_keys(self.email)
        password_input.send_keys(self.password)
        password_input.send_keys(Keys.ENTER)

        time.sleep(5)


my_bot = RobinHoodBot('[email protected]', 'password')
my_bot.login()

Just the reminder to read

https://robinhood.com/robots.txt

Upvotes: 2

Related Questions