1

I'm working on a python script to get some data from a website using selenium Chrome webdriver. Finding the elements needed worked fine for me - until now. Now I'm trying to get an advertisement-ID (value of "data-ad-link".

<div class="header w-brk" style="overflow-wrap: break-word;">
        <a href="/iad/immobilien/d/eigentumswohnung/wien/wien-1010-innere-stadt/am-werdertor-etages-de-luxe-344939582/" class="" data-ad-link="344939582">
            <span itemprop="name">
                AM WERDERTOR - ÉTAGES DE LUXE
            </span>
        </a>
    </div>

From this HTML segment I'd need the value of data-ad-link. I tried solving this problem using

elem = driver.find_elements_by_xpath("//*[@class='']")
for i in range(count):
    #uniqueid = elem[i].get_attribute('data-ad-link')
    #uniqueid = driver.execute_script('var items = {}; for (index = 0; index < arguments[0].attributes.length; ++index) { items[arguments[0].attributes[index].name] = arguments[0].attributes[index].value }; return items;', elem[i])
    print(uniqueid)

In my debugger I'm seeing that creating the list works well - but getting the value doesn't. So I already tried element.get_attribute which returned None (also for href!). I tried driver.execute_script which I found here: Selenium webdriver: How do I find ALL of an element's attributes? which just gives me class, href and rel.

Does anyone know how I could get this value? It would help me so much!

I'm using Selenium (v 3.141.0) on Python

Guy
  • 46,488
  • 10
  • 44
  • 88
aqx
  • 13
  • 2

1 Answers1

0

To extract the value of the attribute data-ad-link i.e. 344939582 you have to induce WebDriverWait for the desired visibility_of_element_located() and you can use the following Locator Strategy:

  • Using XPATH:

    print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='header w-brk']/a[starts-with(@href, '/iad/immobilien/d/eigentumswohnung/wien/wien-')]/span[@itemprop='name' and normalize-space()='AM WERDERTOR - ÉTAGES DE LUXE']/.."))).get_attribute("data-ad-link"))
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you very much for your answer. It works for this specific case. Now my problem is that I want to use this for a whole list where the attributes "href" and "name" are different. I'd need a more generic solution I guess. – aqx Feb 16 '20 at 21:16
  • @aqx Can you raise a new question with your new requirement please? Stackoverflow contributors will be happy to help you out. – undetected Selenium Feb 16 '20 at 21:19