Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python在Selenium webdriver的弹出窗口中单击图标_Python_Selenium_Selenium Webdriver_Xpath_Css Selectors - Fatal编程技术网

如何使用Python在Selenium webdriver的弹出窗口中单击图标

如何使用Python在Selenium webdriver的弹出窗口中单击图标,python,selenium,selenium-webdriver,xpath,css-selectors,Python,Selenium,Selenium Webdriver,Xpath,Css Selectors,我试图通过单击弹出窗口右上角的“X”图标关闭信用卡3DS验证,如下所示 上述弹出窗口的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 heigh

我试图通过单击弹出窗口右上角的“X”图标关闭信用卡3DS验证,如下所示

上述弹出窗口的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>
但是弹出窗口不会关闭

我注意到当我将鼠标悬停在图标上时,光标会指示可以单击图标,这与我的问题有关吗


谢谢您的回答。

要单击元素,您必须引导WebDriverWait使
元素成为可单击的()
,您可以使用以下任一解决方案:

  • 使用
    CSS\u选择器

    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()

我以前也遇到过同样的问题,所以我知道这很沮丧,希望这能有所帮助。

你能分享要爬网的url吗?@anon\u 143很抱歉,我现在不能这么做,因为它与我的软件公司的付款流程有关。当你手动单击它时,它是否关闭?请检查该按钮是否在iframe内,单击该按钮时是否出现异常?在单击之前等待一些时间。如果css选择器不起作用,我建议您使用xpath,或者您知道的任何其他具有类似弹出窗口的随机站点?
driver.find_element_by_css_selector('div#three-ds-container i#close-3ds.fa.fa-close').click()