如何使用 Selenium 和 Python 单击#shadow-root(已关闭)内的按钮

Srk*_*rki 6 python selenium selenium-chromedriver selenium-webdriver shadow-dom

我正在尝试单击#shadow-root (closed)iframe 内的按钮

<iframe title="recaptcha challenge expires in two minutes" 
   <div class="button-holder help-button-holder">
      #shadow-root (closed)
         <link rel="stylesheet" href="chrome-extension://mpbjkejclgfgadiemmefgebjfooflfhl/src/solve/solver-button.css">
         <button tabindex="0" title="Solve the challenge" id="solver-button"></button>
   </div>
</iframe>
Run Code Online (Sandbox Code Playgroud)

这就是我用来切换到 iframe 的方法

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@title='recaptcha challenge expires in two minutes']")))
Run Code Online (Sandbox Code Playgroud)

我怎样才能点击//button[@id="solver-button"]

这是它的真实外观的照片

iframe-链接

按钮链接

Deb*_*anB 1

您切换到的代码行<iframe>非常完美。但是,所需的元素位于#shadow-root(封闭)内,并且使用Selenium您将无法与#shadow-root(封闭)内的元素进行交互,因为根据Shadow DOM 遍历支持中的讨论:

  • @43081j 提到,对于封闭的影子根,应该接受您无法访问它(抛出异常)。因此,我们不需要关心封闭的根,因为无论如何我们都不应该访问它们。
  • @AutomatedTester 在他的规范提案中建议能够在 Shadow DOM 上下文和文档上下文之间移动。