I
I
Ivan Yakushenko2019-05-27 12:21:39
Python
Ivan Yakushenko, 2019-05-27 12:21:39

Why can't I get element attribute in Selenium?

Fragment of HTML code
<div class="_2EZ_m" data-ref="[email protected]+i8eamrYWcD7C0BZyGrmEGXcEvBs">
    <span>
    </span>
    <div class="_1jjYO">
        <span class="_3CSsZ">
            <svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" viewBox="0 0 64 64">
                <path fill="#FFF" d="M6.525 43.936a29.596 29.596 0 0 1-3.039-13."></path>
                <path fill="#123033" d="M50.801 13.135c-4.739-4.742-11.039-7.354-17.752-7.357-13"></path>
                <path fill="#123033" d="M25.429 19.26a8.65 8.65 0 0 0-1.028.011 2.352 2.352 0 0 0-.95."></path>
            </svg>
        </span>
    </div>
    <canvas width="264" height="264" style="display: none;"></canvas>
    <img alt="Scan me!" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQgAAAEICAYAAACj9mr/AA" style="display: block;"></div>

XPath to it: //*[@id="app"]/div/div/div[2]/div[1]/div/div[2]/div
Check for element search works, element is:
try:
    driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div[1]/div/div[2]/div')
except NoSuchElementException as e:
    print(e.msg)

I'm trying to take its attribute:
qr = driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div[1]/div/div[2]/div')
print(qr.get_attribute('data-ref'))

Always outputs None.
UPD.
I tried searching by class name:
qr = driver.find_element_by_class_name('XSdna')
print(qr.get_attribute("innerHTML"))

Conclusion:
<div class="_2EZ_m"><svg class="_1UDDE" width="65" height="65" viewBox="0 0 44 44"><circle class="_3GbTq _3AnXT" cx="22" cy="22" r="20" fill="none" stroke-width="4"></circle></svg></div>

There is no data-ref. I look at the site through code review - there is, but selenium says no. Both selenium and I use the same browser. How is this even possible?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2019-05-27
@kshnkvn

I recommend doing a direct attribute lookup rather than chaining it. This approach will ensure that your attribute is found.
I also recommend doing print(qr.text) before an explicit search to make sure that he found the necessary piece
. You can try .get_attribute("innerHTML")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question