0

I was watching a YouTube Tutorial video about Selenium for python Python Selenium Tutorial - Tech with Tim. The video was uploaded 2 years ago and it seems there are changes in Selenium for python (the find_elements_by_... functions were replaced by find_elements(by = ...) ). My problem is, when I try to convert the code, I get an AttributeError as: AttributeError: 'list' object has no attribute 'find_elements'. I am sharing both the old and new version of the code. What is the difference between the old version and why am I getting the following error? How can I fix it? Thanks in advance!

###Old Version###
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

PATH = "my_path"
driver = webdriver.Chrome(PATH)

driver.get("https://techwithtim.net")

search = driver.find_element_by_name("s")
search.send_keys("test")
search.send_keys(Keys.RETURN)

try:
    main = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.ID, "main")))
    articles = main.find_element_by_class_name("entry-title")

finally:
    driver.quit()
###My New Version###
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

PATH = "my_path"
driver = webdriver.Chrome(PATH)

driver.get("https://techwithtim.net")

search = driver.find_element(By.NAME, value = "s")
search.send_keys("test")
search.send_keys(Keys.RETURN)

try:
    main = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.ID, "main")))
    articles = main.find_elements(By.CLASS_NAME, "entry-title")

finally:
    driver.quit()

In the video, the instructor was trying to get articles in ID:main. However, when I try to rewrite the code, I get Attribute Error.

2
  • It is unclear where your error gets raised, seems like some code is missing. Anyway if the instructor was searching by ID and you use literaly anything else, the problems is, that only 1 element can have searched ID while for eg. class can be found in multiple elements, therefore you are returned a list of all those elements. You can check it by for example if isinstance(articles, list): .... My advice is to use IDE that lets you inspect the content of variables (I recommend pycharm). Also have you checked the source code of the page that the ID you search is there? Commented Dec 9, 2022 at 13:54
  • learn.microsoft.com/en-us/power-automate/desktop-flows/… Commented Dec 9, 2022 at 13:59

1 Answer 1

1

find_elements() method can be only applied on WebDriver object or on WebElement object.
Here main = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.ID, "main"))) main is a list of WebElement objects. So, to apply find_elements you have to use another expected_conditions like presence_of_element_located so that main will be a single WebElement object

main = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "main")))
articles = main.find_elements(By.CLASS_NAME, "entry-title")

or to extract single element from the list, like this:

main = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.ID, "main")))
articles = main[0].find_elements(By.CLASS_NAME, "entry-title")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.