Home > OS >  How to get find_elements_by_class_name to work in Python Selenium?
How to get find_elements_by_class_name to work in Python Selenium?

Time:11-04

from bs4 import BeautifulSoup
import requests
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

url = "https://www.staples.ca/products/959340-en-dr-pepper-355-ml-cans-12-pack"

s=Service('C:/Users/teddy/OneDrive/Desktop/chromedriver.exe')
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--incognito')
options.add_argument('--headless')
driver = webdriver.Chrome(service=s)

driver.get(url)
get_elements = driver.find_elements_by_class_name()

My Python IDE (Pycharm) shows the find_elements_by_class_name as crossed out like driver.find_elements_by_class_name . I thought it was because it was deprecated but that's the only recommendation it gives me when I type driver.find... I did some Googling, tried findElement, find_Element, none of it seems to work.

Hopefully somebody will be able to help me.

Thanks

CodePudding user response:

Please try with find_elements(By.CLASS_NAME,..) and see if the error persists.

from selenium.webdriver.common.by import By

driver.find_elements(By.CLASS_NAME, '//button')

Source: Documentation

CodePudding user response:

If you are using the Selenium V3.0 series, it has support for both

get_elements = driver.find_elements_by_class_name()

or an equivalent

get_elements = driver.find_elements(By.CLASS_NAME, "")

both should work.

Also, not exactly sure why you are facing that issue. Please clarify your Selenium, Python, driver version etc.

Fix :

You can try, explicit waits to have a list like this :

get_elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "")))

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
  • Related