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.
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?