我正在使用Python学习Selenium,并尝试为网站编写快乐的路径流。我卡住的地方是流程的一部分,在其中单击弹出窗口后,会自动启动iframe。我尝试了几种不同的方法,但是无法找到框架或等待其出现。找不到或超时。
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.ID, 'continue- reservation'))).click()
# cvv2 form
WebDriverWait(driver, 5).until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'cvv_iframe')))
错误:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'cvv_iframe')))
File "/Users/anutter/venv/lib/python3.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
HTML如下:
<div id="cpstaging" class="emptystage">
<div class="fixed h-v-centered new-preload" style="display: none;"><span class="fa fa-spinner f-50 f-grn fa-pulse"></span></div>
</div>
<div id="cvv-box" class="pad-30-lr">
<div id="enter-cvv-title" class="blk mar-20-b f-18 border-b pad-10-b">Enter CVV Code</div>
<iframe src="https://qa-hotels.ecbsn.com/cvv?oauth_token=u7q99%2Fe8I%2BkUrkLMr4dGR2t4gmcDbVtr&type=visa&src=web-desktop" id="cvv_iframe" name="cvv_iframe" width="430" height="160" frameborder="0" seamless="seamless" scrolling="no" sandbox="allow-same-origin allow-scripts allow-popups allow-forms"></iframe>
</div>
</div>
</div>
</div>
最佳答案
按照您共享的HTML来访问<iframe>
中的元素,您必须:
诱导WebDriverWait使所需的帧可用,并使用Locator Strategies之一切换到它:
使用ID
:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"cvv_iframe")))
使用
NAME
:WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"cvv_iframe")))
使用
CSS_SELECTOR
:WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#cvv_iframe[name='cvv_iframe']")))
使用
XPATH
:WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='cvv_iframe' and @name='cvv_iframe']")))
注意:根据您的代码试用版,
<iframe>
的ID不是cvv-iframe
,而是cvv_iframe
在这里您可以找到有关Ways to deal with #document under iframe的相关讨论
关于python-3.x - 等待可见的iframe超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53529836/