【问题标题】:Element not being clicked even though it is found using Selenium即使使用 Selenium 找到元素也没有被点击
【发布时间】:2020-07-28 23:05:39
【问题描述】:

我正在尝试使用 Selenium(在 Python 中)单击一个元素(单选按钮),我无法透露 URL,因为它是一个私有的公司内部网,但会共享代码的相关部分。

所以基本上这个元素在 iframe 中,因此,我使用以下代码来获取元素:

# Select the item on main DOM that will udpate the iframe contents
wait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='sm20']"))).click()
# Don't sleep, but only WedDriverWait...
wait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@name='ifrInterior']")))
# Select the element inside the iframe and click
wait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='gestionesPropias_Equipo']"))).click()

该元素的 HTML 代码如下:

<span class="W1">
    <input type="radio" name="gestionesPropias_Equipo" value="1" onclick="javascript:indicadorPropiasEquipo(); ocultarPaginacion('1'); limpiarDatosCapaResultado();">
</span>

我有兴趣点击它,因为当我们点击它时,会启用下拉菜单:

如果单击则启用下拉菜单:

有趣的 HTML 代码是

<span id="filtroAsignado" class="W30">
            
    <select name="nuumaAsignado" class="W84">       
        <option value="">[Todo mi equipo]</option></select>
            
</span>

调试一下 Selenium 我可以看到找到了elemtn:

而这实际上是元素的base64图像,也就是预期的单选按钮

所以我想知道为什么元素实际上没有被点击??

更新:根据@DebanjanB 的请求,我正在从 iframe 添加 HTML 代码,该代码包含在主页的 div 中:

<div id="contenido">
        <iframe frameborder="0" id="ifrInterior"  name="ifrInterior" src="Seguimiento.do?metodo=inicio" scrolling="auto" frameborder="0"></iframe>
</div>

其实如果我寻找“iframe”这个词,只有一个……

现在检查 iframe 源本身,隐藏了几个 iframe,但我需要与之交互的元素在上面提到的 iframe 中,我唯一忘记提及的是它在表单中,但我想这不相关?您可以在下图中看到整个结构:

【问题讨论】:

    标签: python selenium selenium-webdriver xpath webdriverwait


    【解决方案1】:

    很好的问题。如果您知道 selenium 找到了该元素,则可以使用 Javascript 直接单击该元素。 语法是:

    driver.execute_script("arguments[0].click();", element)
    

    您也可以这样做,其工作方式相同:

    automate.execute_script("arguments[0].click();", wait.until(EC.element_to_be_clickable((By.XPATH, 'Your xpath here'))))
    

    基本上你是让 Selenium 运行一个 javascript 点击你找到的绕过 Selenium 的元素。让我知道这是否有帮助!

    【讨论】:

      【解决方案2】:

      我也没有看到您的代码块有任何此类问题。也许您可以尝试以下任一选项:

      • 使用动作链

        ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='gestionesPropias_Equipo' and @type='radio']")))).click().perform()
        
      • 使用executeScript()方法:

        driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='gestionesPropias_Equipo' and @type='radio']"))))
        

      【讨论】:

      • 使用 executeScript 会遇到 selenium.common.exceptions.NoSuchWindowException: 消息:没有这样的窗口异常 实际上 ActionChains 也会发生同样的情况......感觉我在这里绕圈子跑:D
      • @AlejandroVK 请交叉检查该元素是否在另一个子框架内?
      • 编辑了更多信息的描述,如您所见,它在该框架内...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多