Home > front end >  Python Selenium login without using id tag
Python Selenium login without using id tag

Time:01-11

I am using Selenium to try and auto login. The driver has methods for id but which method do I use if the html page does not have id?

<input type="text" placeholder="Enter ID here (Example - someuser)" name="LoginID" required="">
<input type="password" placeholder="Enter your Password here" name="Password" required="">
<button type="submit">SUBMIT</button>

Currently I have this but I need to replace the id method of course just not sure to what

def login(url):
    driver.get(url)
    driver.find_element_by_id("text").send_keys("MyuserName")
    driver.find_element_by_id("password").send_keys("somepassword")
    driver.find_element_by_id("submit").click()

login(url)

CodePudding user response:

Those methods have been deprecated:

warnings.warn(
        "find_element_by_* commands are deprecated. Please use find_element() instead",
        DeprecationWarning,
        stacklevel=2,
    )

You may use the following:

find_element(self, by=By.ID, value=None)

The possible search locator are as follow:

class By(object):
    """
    Set of supported locator strategies.
    """

    ID = "id"
    XPATH = "xpath"
    LINK_TEXT = "link text"
    PARTIAL_LINK_TEXT = "partial link text"
    NAME = "name"
    TAG_NAME = "tag name"
    CLASS_NAME = "class name"
    CSS_SELECTOR = "css selector"

For your code try the following:

from selenium.webdriver.common.by import By
search_container = driver.find_element(By.CSS_SELECTOR, 'input[name="LoginID"]')
  •  Tags:  
  • Related