0

This is my python/selenium code

username = driver.find_element_by_xpath('//*[@id="username"]')
password = driver.find_element_by_xpath('//*[@id="password"]')
enter = driver.find_element_by_xpath('//*[@id="loginbox"]/input[3]')

passfile = open(alltools.get_letter() + "path\pass.txt", "r")
line = passfile.readlines()

username.send_keys(line[0])
password.send_keys(line[1])
enter.click()

The form I'm trying to fill is

<form style="align: center;" id="loginForm" method="post" action="j_security_check" autocomplete="off">
      <div class="identity-box" id="loginbox">
        <div class="alert-error" style="display: none">Invalid username or password</div>
        <label class="label" for="username">Username</label>
        <input type="text" class="text-input" size="30" name="j_username" id="username">
        <label class="label" for="password">Password</label>
        <input type="password" class="text-input" size="30" name="j_password" id="password">
        <input type="submit" class="button" value="Sign In">
      </div>
    </form>

I know the issue is with the autocomplete="off". How do I turn it off?

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
thepepp
  • 164
  • 1
  • 1
  • 11

1 Answers1

0

autocomplete Attribute

Generally autocomplete attribute is used to turn off autocomplete for input fields. As an example:

<input type="text" autocomplete="off">

autocomplete can also be turned off autocomplete for the whole form:

<form autocomplete="off">

This usecase

As per the HTML you have shared is turned off at the <form> level:

<form style="align: center;" id="loginForm" method="post" action="j_security_check" autocomplete="off">

You should be good to go.

To send a character sequence to the Username field you can use either of the following Locator Strategies:

  • Using css_selector:

    driver.find_element_by_css_selector("label[for='username']").send_keys("thepepp")
    
  • Using xpath:

    driver.find_element_by_xpath("//label[@for='username']").send_keys("thepepp")
    

Ideally, to send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='username']"))).send_keys("thepepp")
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@for='username']"))).send_keys("thepepp")
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • and yet it does not work, it might be the action="j_security_check" as it shows up in the link after python attempts to populate the form. – thepepp Jan 04 '21 at 23:34
  • @thepepp Checkout the updated answer and let me know the status. – DebanjanB Jan 05 '21 at 06:27