我正在尝试通过单击弹出窗口右上方的'X'图标来关闭信用卡3DS验证,如下所示。
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLmltZ3VyLmNvbS9YdnZRVjFILnBuZyJ9” alt =“ 3DS Pop-up”>
以上弹出窗口的CSS代码在这里:
<div id="three-ds-container" style="">
<i id="close-3ds" style="position: absolute;right: 10px;top: 10px;cursor:pointer;" class="fa fa-close"></i>
<iframe height="450" width="550" id="sample-inline-frame" name="sample-inline-frame"></iframe>
</div>
我尝试通过使用单击图标
driver.find_element_by_css_selector('div#three-ds-container i#close-3ds.fa.fa-close').click()
但是弹出窗口不会关闭。
[我注意到当我将鼠标悬停在图标上时,光标会指示可以单击该图标,这与我的问题有关吗?
感谢您的回答。
要单击元素,您必须为element_to_be_clickable()
引入WebDriverWait,并且您可以使用以下解决方案之一:
使用CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#three-ds-container > i.fa-close#close-3ds"))).click()
使用XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='three-ds-container']/i[@class='fa fa-close' and @id='close-3ds']"))).click()
注:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
弹出窗口与您所在的框架不同,所以我认为您的问题是您需要切换到该框架(iframe height =“ 450” width =“ 550” id =“ sample-inline-frame” name = “ sample-inline-frame”)以单击关闭按钮。
执行此操作的代码如下所示:
iframe = driver.find_element_by_id("sample-inline-frame")
driver.switch_to.frame(iframe)
那么您应该可以:
driver.find_element_by_css_selector('div#three-ds-container i#close-3ds.fa.fa-close').click()
我之前遇到过同样的问题,所以我很沮丧,希望这会有所帮助。