-1

I'm creating a program that will help students track their marks, for that I need to web scrap the site where all our grades are stored. The idea was to automate the login and then there will be a separate script that will get the information that I need. I have trouble with automated login, because there is no input field and button ID. All I could find was names input fields and class for the button.

browser = webdriver.Chrome()
browser.get(("https://www.the-site.com/"))

usernameStr = "myUsername"
passwordStr = "myPassword"

username = browser.find_element("UserName")
username.send_keys(usernameStr)
password = browser.find_element("Password")
password.send_keys(passwordStr)
nextButton = browser.find_element("btn btn-default")
nextButton.click()

This is the error that I get:

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator

Here's the HTML for the input fields and button:

 <form action="https://my.e-klase.lv?v=15" method="post" autocomplete="off">
            <div class="form-group relative">
                <input type="text" class="form-control" placeholder="Lietotājvārds"
                       autocomplete="off"
                       autocorrect="off"
                       autocapitalize="off"
                       spellcheck="false" name="UserName"
                       value="">
                <div class="dummy-placeholder">Lietotājvārds</div>
            </div>
            <div class="form-group relative">
                <input type="private" class="form-control upass"
                       placeholder="Parole"
                       autocomplete="off"
                       autocorrect="off"
                       autocapitalize="off"
                       name="Password">
                <div class="dummy-placeholder">Parole</div>
            </div>

            <button type="submit" class="btn btn-default">
                <span class="progress"></span>
                <span class="content">Pieslēgties</span>
            </button>

        </form>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

1

find_element()

find_element() finds an element given a By strategy and a locator.

You code was missing the By strategy and locator. Hence you see the error:

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator

This usecase

As per the html you have shared, to fill in the UserName and Password field ideally you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

browser = webdriver.Chrome()
browser.get(("https://www.the-site.com/"))
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.NAME, "UserName"))).send_keys("myUsername")
browser.find_element(By.NAME, "Password").send_keys("myPassword")
browser.find_element(By.XPATH, "//button[@class='btn btn-default']//span[text()='Pieslēgties']").click()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

You can locate the elements based on any their unique attributes.
It can be id, class name(s), name and any other attribute values.
To give you more specific answer we need to see that web page elements HTML.
UPD
According to your HTML you can use the following locators:

browser = webdriver.Chrome()
browser.get(("https://www.the-site.com/"))

usernameStr = "myUsername"
passwordStr = "myPassword"
time.sleep(5)

username = browser.find_element_by_xpath("//input[@name='UserName']")
username.send_keys(usernameStr)
password = browser.find_element_by_xpath("//input[@name='Password']")
password.send_keys(passwordStr)
nextButton = browser.find_element_by_xpath("//button[@type='submit']")
nextButton.click()

This should basically work, but you should use ExpectedConditions explicit wait in order to wait for the page loaded

Prophet
  • 32,350
  • 22
  • 54
  • 79