Search code examples
pythonselenium-webdriverselenium-chromedriverbrowser-automationfindelement

Why don't I have find_element_by_blablabla?


I was using Selenium to automate browser things and I my WebDriver object doesn't have the attributes find_element_by_link_text for example.

It only has:

find_element()

or:

find-elements()

No other methods starting with find. Is this due to my WebDriver?

I was using:

  • Web Driver: Chrome
  • Version: 103

Solution

  • based on the Selenium Python documentation, first you need to import By :

    from selenium.webdriver.common.by import By
    

    Then, depending on how you want to locate a certain element, these are the following available attributes:

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

    And the syntax to locate elements using the above mentioned attributes is the following:

    find_element(By.ID, "id")
    find_element(By.NAME, "name")
    find_element(By.XPATH, "xpath")
    find_element(By.LINK_TEXT, "link text")
    find_element(By.PARTIAL_LINK_TEXT, "partial link text")
    find_element(By.TAG_NAME, "tag name")
    find_element(By.CLASS_NAME, "class name")
    find_element(By.CSS_SELECTOR, "css selector")
    

    I hope this answers your question!