即使使用硒找到元素也未单击

时间:2020-07-28 23:05:39

标签: python selenium selenium-webdriver xpath webdriverwait

我正在尝试使用Selenium(在Python中)单击某个元素(单选按钮),由于它是私有公司Intranet,所以无法公开该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>

我对此感兴趣,因为当我们单击它时,将启用一个下拉列表:

enter image description here

如果单击,则启用下拉列表:

enter image description here

有趣的HTML代码是

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

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

enter image description here

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

enter image description here

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

更新:根据@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中,我唯一忘记提及的是它位于表单中,但我想这无关紧要?您可以在下图中看到整个结构:

enter image description here

2 个答案:

答案 0 :(得分:1)

很好的问题。如果您知道硒找到了元素,则可以使用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的元素。让我知道这是否有帮助!

答案 1 :(得分:0)

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

  • 使用 ActionChains

    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']"))))