<p>此错误消息。。。在</p>
<pre><code>selenium.common.exceptions.ElementNotInteractableException: Message: Element <span class="ui-button-text"> could not be scrolled into view
</code></pre>
<p>…意味着<strong>WebDriver</strong>实例,即<em>驱动程序</em>无法滚动<a href="https://www.w3schools.com/css/css_rwd_viewport.asp" rel="noreferrer">Viewport</a>中的元素来调用<code>click()</code>。在</p>
<hr/>
<p>首先,由于您的用例是在元素上调用<code>click()</code>,理想情况下不是使用<code>presence_of_element_located()</code>,您需要使用<em>ExpectedConditions</em>作为<strong><code>element_to_be_clickable()</code></strong>:</p>
^{pr2}$
<p>您可以在以下位置找到一些详细讨论:</p>
<ul>
<li><a href="https://stackoverflow.com/questions/51250668/message-element-option-could-not-be-scrolled-into-view-while-trying-to-click/51252175#51252175">Message: Element could not be scrolled into view while trying to click on an option within a dropdown menu through Selenium</a></li>
<li><a href="https://stackoverflow.com/questions/51025144/org-openqa-selenium-elementnotinteractableexception-element-a-class-bg-invers/51025869#51025869">org.openqa.selenium.ElementNotInteractableException: Element could not be scrolled into view when trying to click a button</a></li>
</ul>
<hr/>
<p>另一种方法是,根据错误消息,在调用^{<cd1>之前,在<strong>视口中滚动一个元素</strong>,还可以使用<a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView" rel="noreferrer">Element.scrollIntoView()</a>方法。在</p>
<p>详细讨论请参见:
-<a href="https://stackoverflow.com/questions/54681172/what-is-the-difference-between-the-different-scroll-options/54689339#54689339">What is the difference between the different scroll options?</a></p>
<hr/>
<p>在这一点上值得一提的是,以下方法:</p>
<ul>
<li><a href="https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.action_chains.html#selenium.webdriver.common.action_chains.ActionChains.move_to_element" rel="noreferrer">move_to_element()</a>来自<a href="https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.action_chains.html#module-selenium.webdriver.common.action_chains" rel="noreferrer">selenium.webdriver.common.action_chains</a></li>
<li><a href="https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html#selenium.webdriver.support.expected_conditions.element_to_be_clickable" rel="noreferrer">element_to_be_clickable()</a>来自<a href="https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html#module-selenium.webdriver.support.expected_conditions" rel="noreferrer">selenium.webdriver.support.expected_conditions</a></li>
</ul>
<p>将自动滚动<em>视口</em>中的元素。在</p>
<p>详细讨论请参见:
-<a href="https://stackoverflow.com/questions/55361795/how-to-scroll-a-webpage-using-selenium-webdriver-in-python-without-using-javascr/55587059#55587059">How to scroll a webpage using selenium webdriver in Python without using javascript method execute_script()</a></p>
<hr/>
<h2>本用例</h2>
<p>文本为<strong>继续</strong>的按钮位于<em>顶层内容</em>中,但在<em>模式对话框</em>中呈现。在</p>
<p>开发工具快照:</p>
<p><a href="https://i.stack.imgur.com/joQ8E.png" rel="noreferrer"><img src="https://i.stack.imgur.com/joQ8E.png" alt="ModalDialogBox"/></a></p>
<p>由于所需元素位于<em>模式对话框</em>中,因此要定位并调用元素上的<code>click()</code>,您必须为<code>element_to_be_clickable()</code>归纳<em>WebDriverWait</em>,并且可以使用以下<a href="https://stackoverflow.com/questions/48369043/official-locator-strategies-for-the-webdriver/48376890#48376890">Locator Strategy</a>:</p>
<ul>
<li><p>使用<code>XPATH</code>:</p>
^{3美元</li>
<li><p><strong>注意</strong>:您必须添加以下导入:</p>
<pre><code>from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
</code></pre></li>
</ul>
<p>开发工具快照:</p>
<p><a href="https://i.stack.imgur.com/SNtvz.png" rel="noreferrer"><img src="https://i.stack.imgur.com/SNtvz.png" alt="XPath"/></a></p>