2

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!

2 Answers 2

1

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()
Sign up to request clarification or add additional context in comments.

1 Comment

This works! There was half the answer in a link in the documentation (saucelabs.com/resources/articles/selenium-tips-css-selectors) but it did not mention anything about replacing blank space with dots. Thanks a lot. Have a nice day!
1

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

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.