0

I am running a little Python Selenium script and I want to access attributes from the first element on this site: https://www.mydealz.de/gruppe/spielzeug. Every few minutes the first element is different and has therefore a different Xpath identifier. What are the possibilites to access all the time this first element, which has different id's/Xpaths? The first result I meant.

Thanks a lot in advance!

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Luke
  • 11
  • 6

2 Answers2

0

I've keep an eye open on the website for the last 15 minutes, but for me the page has not changed.

Nevertheless, I tried to scrape the data with BS4 (which you could populate with Selenium's current browser session), where it should always return the first element first.

from bs4 import BeautifulSoup
import requests

data = requests.get('https://www.mydealz.de/gruppe/spielzeug')
soup = BeautifulSoup(data.text, "html.parser")
price_info = soup.select(".cept-tp")

for element in price_info:
    for child in element:
        print(child)

Of course this is just for the price, but you can apply the same logic for the other elements.

0

To print the first title you have to induce WebDriverWait for the desired visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.threadGrid div.threadGrid-title.js-contextual-message-placeholder>strong.thread-title>a"))).get_attribute("title"))
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='threadGrid']//div[@class='threadGrid-title js-contextual-message-placeholder']/strong[@class='thread-title']/a"))).text)
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Console Output of two back to back execution:

  • [Mediamarkt @Ebay.de] diverse Gravitrax Erweiterungen günstig!

  • [Mediamarkt @Ebay.de] diverse Gravitrax Erweiterungen günstig!

As per the documentation:

  • get_attribute(name) method Gets the given attribute or property of the element.

  • text attribute returns The text of the element.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352