Search code examples
pythonseleniumselenium-webdriverwebdriver

Having problem with the usage of driver find element by (Selenium)


I'am trying to make a bot that auto logins to a website. In order to write the username I'm trying to use

driver.find_element_by_variable("username").send_keys(username)

When I'm looking that spesific variable from website on inspect the varible is two word like matinput formcontrolname. On any other website if that varible is one word like id I simply write id after by_ and it works what can I do in this situation?


Solution

  • Selenium won't allow you to use:

    find_element_by_variable()
    

    but you have to use either of the predefined Locator Strategies as listed in the By implementation which are as follows:

    • CLASS_NAME= class name

      driver.find_element(By.CLASS_NAME, "element_classname")
      
    • CSS_SELECTOR= css selector

      driver.find_element(By.CSS_SELECTOR, "element_css_selector")
      
    • ID= id

      driver.find_element(By.ID, "element_id")
      
    • LINK_TEXT= link text

      driver.find_element(By.LINK_TEXT, "element_link_text")
      
    • NAME= name

      driver.find_element(By.NAME, "element_name")
      
    • PARTIAL_LINK_TEXT= partial link text

      driver.find_element(By.PARTIAL_LINK_TEXT, "element_partial_link_text")
      
    • TAG_NAME= tag name

      driver.find_element(By.TAG_NAME, "element_tag_name")
      
    • XPATH= xpath

      driver.find_element(By.XPATH, "element_xpath")