Home > OS >  Why don't I have find_element_by_blablabla?
Why don't I have find_element_by_blablabla?

Time:07-18

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

CodePudding user response:

The Newer Versions of Selenium in Python Are Using the By Module

Try something like

    from selenium.webdriver.common.by import By
    webdriver # Lets assume it exists
    oneElement=webdriver.find_element(By.ID, "id")
    youWanted=find_element(By.LINK_TEXT, "link text")
    listOfMatchingElements=webdriver.find_elements(By.XPATH, '//button')

The other "find_elements_by_*" are old and not longer used For Reference look here https://selenium-python.readthedocs.io/locating-elements.html

CodePudding user response:

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!

  • Related