'python selenium find elements by xpath returns a list of a links but element not interactable

I am using this xpath //a[contains(., 'Download Python')] on www.python.org/downloads

in chrome developer tools I get 4 matches, each one match the following

<a class="button" href="https://www.python.org/ftp/python/3.7.4/python-3.7.4-macosx10.9.pkg">Download Python 3.7.4</a>
<a class="button" href="https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tar.xz">Download Python 3.7.4</a>
<a class="button" href="https://www.python.org/ftp/python/3.7.4/python-3.7.4.exe">Download Python 3.7.4</a>
<a class="button" href="https://www.python.org/ftp/python/3.7.4/python-3.7.4.exe">Download Python 3.7.4</a>

the problem is only the third is clickable, the others give an error ElementNotInteractableException: Message: element not interactable

if I print the text of each element only the third one displays the text 'Download Python 3.7.4' the rest print blank

elems = driver.find_elements_by_xpath(
    "//a[contains(., 'Download Python')]")
for elem in elems:
    print("-------")
    print(elem.text)
    print(elem.tag_name)
-------

a
-------

a
-------
Download Python 3.7.4
a
-------

a

as a result only elems[3].click works



Solution 1:[1]

This problem is not with your code, the python download page detects the OS you are browsing from and shows you the download link for that OS only. It has total 4 such buttons

        <div class="download-os-mac-osx" style="display: none;">

            <h1 class="call-to-action">Download the latest version for Mac OS X</h1>

            <p class="download-buttons">

                <a class="button" href="https://www.python.org/ftp/python/3.7.4/python-3.7.4-macosx10.9.pkg">Download Python 3.7.4</a>

            </p>
        </div>

        <div class="download-os-source" style="display: none;">

            <h1 class="call-to-action">Download the latest source release</h1>

            <p class="download-buttons">

                <a class="button" href="https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tar.xz">Download Python 3.7.4</a>

            </p>
        </div>

        <div class="download-os-windows" style="">

            <h1 class="call-to-action">Download the latest version for Windows</h1>

            <p class="download-buttons">

                <a class="button" href="https://www.python.org/ftp/python/3.7.4/python-3.7.4.exe">Download Python 3.7.4</a>

            </p>
        </div>

    <div class="download-unknown" style="display: none;">
        <h1 class="call-to-action">Download the latest version of Python</h1>
        <p class="download-buttons">

            <a class="button" href="/downloads/release/python-374/">Download Python 3.7.4</a>

        </p>
    </div>

You need to select the one which has style = "" rest others wont be displayed on the page.

Solution 2:[2]

You can also use the xpath with index and it will point to the button corresponding to the index

(Xpath)[index]

Eg: (//*[@class="button"])[0] -for first button

Let me know if it was helpful

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 neeraj jain
Solution 2 Ayaz Mansur