Selenium Message: no such element: Unable to locate element but locator are right

Viewed 26

I want to scrap pinterest board title(s) (https://pl.pinterest.com/ktmac10/_saved/) but nothing works. Why ? I've already tried :

self.driver.find_element(By.CSS_SELECTOR, 'h2[class="lH1 dyH iFc H2s bwj O2T zDA IZT CKL"]').text

self.driver.find_element(By.XPATH, '//h2[@class="lH1 dyH iFc H2s bwj O2T zDA IZT CKL"]').text

Yes, I added implicitly wait and still is the same

1 Answers

Here is a way to get those titles:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd

import time as t

chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')

chrome_options.add_argument("window-size=1280,720")

webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)
wait = WebDriverWait(browser, 20)
url = 'https://pl.pinterest.com/ktmac10/_saved/'
browser.get(url)

titles = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, 'div[data-test-id="board-card-title"]')))
for t in titles:
    print(t.text)

Result printed in terminal:

House Goals
Mani/Pedi
HAIR
Holidays
Food & Drink
Beauty & Makeup
Tattoos
Plants
Accessorize

Selenium setup is on linux/chromedriver, you can adapt the code to yours, just see the imports and the code after defining the browser/driver. Selenium docs can be found at https://www.selenium.dev/documentation/

Related