无法使用 Python Selenium 单击 ahref link

Unable to click on an ahref link using Python Selenium

这是html代码:

<div id="dcf-page-export-buttons" class="no-print" style="display: block;">
                            <a id="dcf-saveaspdf" href="#" target="_blank" class="k-button">

                                Save as PDF
                            </a>
                            <a id="dcf-saveaspng" href="#" target="_blank" class="k-button">

                                Save as Image
                            </a>
                                                  <a id="dcf-printPdf" class="k-button" href="#">

                                Print
                            </a>
                        <a id="dcf-btnClose" class="k-button" href="#">

                            Close
                        </a>
                   </div>

我想单击“打印 href”,但没有被单击。这是我的代码:

exportLink = driver.find_element_by_link_text("Export")
exportLink.click()

print = driver.find_element_by_id("dcf-printPdf")
print.click()

在按 id 查找要打印的元素之前,我单击了导出 href,它打开了一个新选项卡,在打开新选项卡后,我试图单击打印但出现错误。这是错误:

selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

如果我哪里出错了或者 html 中有问题,请告诉我。


问题的第一部分已经回答。这是第二部分:

点击打印按钮:

这个 window 被打开了。它本身并不是一个新标签,而只是一个新的 window。在 window 中,我想单击“保存”按钮。有办法去做吗?这是视图的样子:

这里是 html 代码。

<cr-button class="action-button" aria-disabled="false" role="button" tabindex="0">
    Save
  </cr-button>

到目前为止,这是我的代码:

exportLink = driver.find_element_by_link_text("Export")
exportLink.click()

driver.switch_to.window(driver.window_handles[1])
driver.execute_script("document.getElementById('dcf-user-info').style.display = 'none';")

time.sleep(1)
print = driver.find_element_by_link_text("Print")
print.click()

这是错误日志的片段。我添加了剪辑,因为我不确定错误。

错误小续:

您需要先切换到新标签页,然后您可以使用您尝试过的那个 Id 单击“打印”。

像这样切换到新的 windows :

driver.switch_to.window(driver.window_handles[1])

从错误中可以看出,您尝试访问的元素不可交互。
所以,问题不在于 HTML.
我看不到您正在处理的页面,但问题可能是您尝试访问的元素不在视图中。
试试这个

from selenium.webdriver.common.action_chains import ActionChains

print = driver.find_element_by_id("dcf-printPdf")

ActionChains(driver).move_to_element(print).click(button).perform()