cinqrougedesign
cinqrougedesign

Reputation: 47

Python Selenium - error while trying to find element by class name

Here is an exemple of element from a website I am trying to scrape. None of them have ids or names, they just have long garbage name like in the exemple bellow.

<h1 class="product-name__item product-name__item--name" title="BOEUF HACHE MAIGRE 1 LB">BOEUF HACHE MAIGRE 1 LB</h1>

I tried two things and got 2 different errors.

First attempt :

I tried finding it by class name. This result in a timeout exception.

try:
element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CLASS_NAME, "product-name__item product-name__item--name"))

    )
    print(element.text)
finally:
    driver.quit()

Second attempt:

I tried finding it by css selector because I thought there were a problem with the class name containing a blank space.This resulted in the following error: TypeError: 'str' object is not callable

try:
element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located(By.CSS_SELECTOR("[class='product-name__item product-name__item--name']"))

    )
    print(element.text)
finally:
    driver.quit()

Would you have a solution? Thank you and have a nice day!

Upvotes: 2

Views: 997

Answers (2)

KunduK
KunduK

Reputation: 33384

Use following css selector.

try:
  element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".product-name__item.product-name__item--name")))
  print(element.text)
except:
    driver.quit()

Upvotes: 1

SeleniumUser
SeleniumUser

Reputation: 4177

if below solution end up with timeout then check if your element is within iframe.

wait = WebDriverWait(driver, 10)
element=wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "product-name__item product-name__item--name")))
print element.text

XPATH:

   wait = WebDriverWait(driver, 10)
    element=wait.until(EC.element_to_be_clickable((By.XPATH, "//h1[contains(text(),'BOEUF HACHE MAIGRE 1 L')]")))
    print element.text

Note : please add below imports to your solution

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

Upvotes: 1

Related Questions