python硒通过xpath查找元素返回链接列表,但元素不可交互

时间:2019-10-14 11:05:29

标签: python selenium xpath

我正在使用此xpath // a [contains(。,'Download Python')] 在www.python.org/downloads

在chrome开发人员工具中,我有4个匹配项,每个匹配以下项

<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>

问题是只有第三个是可单击的,其他的则给出错误ElementNotInteractableException:消息:元素不可交互

如果我打印每个元素的文本,只有第三个元素显示文本“ Download Python 3.7.4” 其余打印空白

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

因此只有elems [3] .click起作用

1 个答案:

答案 0 :(得分:0)

此问题与您的代码无关,python下载页面检测到您正在浏览的操作系统,并仅显示该操作系统的下载链接。它总共有4个这样的按钮

        <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>

您需要选择一个样式为=“”的样式,其他样式将不会显示在页面上。