即使使用 Selenium 找到元素,也不会单击该元素 [英] Element not being clicked even though it is found using Selenium

查看:22
本文介绍了即使使用 Selenium 找到元素,也不会单击该元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Selenium(在 Python 中)点击一个元素(单选按钮),我不能透露 URL,因为它是一个私人企业内部网,但会共享代码的相关部分.

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

# 在主 DOM 上选择将更新 iframe 内容的项目wait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='sm20']"))).click()#不要睡觉,但只有WedDriverWait...wait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@name='ifrInterior']")))#选择iframe里面的元素并点击wait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='gestionesPropias_Equipo']"))).click()

元素的 HTML 代码是这样的:

<输入类型=收音机"名称=gestionesPropias_Equipo"值=1"onclick="javascript:indicadorPropiasEquipo();ocultarPaginacion('1');limpiarDatosCapaResultado();></span>

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

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

这个有趣的 HTML 代码是

<select name="nuumaAsignado";class=W84"><option value=">[Todo miequipo]</option></select></span>

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

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

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

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

<iframe frameborder="0";id=ifrInterior"名称=ifrInterior"src=Seguimiento.do?metodo=inicio"滚动=自动"frameborder=0"></iframe>

实际上,如果我寻找iframe"这个词,只有一个......

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

解决方案

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

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

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

automate.execute_script("arguments[0].click();", wait.until(EC.element_to_be_clickable((By.XPATH, '你的 xpath 在这里'))))

本质上,您是让 Selenium 在您找到的绕过 Selenium 的元素上运行 javascript 单击.如果这有帮助,请告诉我!

I'm trying to click on an element (radio button) using Selenium (in Python), I can't disclose the URL because it's a private corporate intranet, but will share the relevants part of code.

So basically this element is within an iframe, thus, I've used the following code to get the element:

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

The HTML code for the element is this:

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

I'm interested in clicking this because when we click on it a drop-down is enabled:

If clicked then the dropdown is enabled:

The intersting HTML code for this is

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

Debugging a bit Selenium I can see that the elemtn is found:

And this is actually the base64 image of the element, which is the expected radio button

So I'm wondering why the element actually does not get clicked??

UPDATE: Based on request from @DebanjanB, I'm adding the HTML code from the iframe, which is enclosed inside a div in the main page:

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

Actually if I look for the word "iframe", there's only one...

Now checking the iframe source itself, has several iframes hidden but the element I need to interact with is in the iframe mentioned above, the only thing that I forgot to mention is that it's inside a form, but I guess that's not relevant? You can see the whole structure in the following image:

解决方案

Great question. If you know that selenium found the element, you can use Javascript to click the element directly. The syntax is:

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

You can also do this, which works the same way:

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

Essentially you are having Selenium run a javascript click on the element you have found which bypasses Selenium. Let me know if this helps!

这篇关于即使使用 Selenium 找到元素,也不会单击该元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆