如何单击使用 Selenium WebDriver 隐藏的元素? [英] How do you click on an element which is hidden using Selenium WebDriver?

查看:39
本文介绍了如何单击使用 Selenium WebDriver 隐藏的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 WebDriver 和 Python 自动化的网络应用程序.

问题是有一个类似这样的菜单 如果我手动单击箭头按钮,它会扩展到另一个子菜单,我需要从中选择特定字段.

我可以找到第三个菜单,但是当我使用 element.click() 单击它而不是展开菜单并向我显示其子菜单项时,它显示的是所有子菜单的合并内容.

(手动扩展到子菜单是通过实际点击组名前的小箭头图标来实现的)那么我如何实际单击此箭头图标以将组菜单之一展开为子菜单.

如果有帮助,这是对应于第三组菜单的HTML.

<div id="node_3_item" class="treeLabelSelected" style="padding-left: 0px; background-position: 0px -24px;"><span style="background-position: 0px -24px;">XXX 组</span>

<div style="display: none;"></div>

display: none 行实际上隐藏了子菜单(据我所知)

任何有关如何处理的建议将不胜感激.谢谢

注意:我已经解决了几个与隐藏的网络元素交互相关的问题,但它们与我的情况不同.

解决方案

抓取你想要点击的元素:

# 或者使用xparth什么的element = driver.find_element_by_css_selector(css_selector)

使用javascript点击它:

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

注意:我正在使用 jQuery 否则选择它原生的 javascript

I have a web application which I am automating using WebDriver and Python.

The issue is that there is a menu something like this if I click manually on the arrow button it expands to another submenu from where I need to select a particular field.

I can find this third menu but when I click on it using element.click() instead of expanding the menu and showing me its sub menu items it is showing consolidated contents of all the sub menu.

(Manually the expansion to sub menu is achieved by actually clicking on the small arrow icons before the group names) So how do I actually click on this arrow icons to expand one of the group menu into sub menus.

This is the HTML corresponding to third group menu if it helps.

<div id="node_3_item" class="treeLabelSelected" style="padding-left: 0px; background-position: 0px -24px;">
<span style="background-position: 0px -24px;">XXX Groups</span>
</div>
<div style="display: none;"></div>
</div>

The display: none line is actually hiding the sub menu (as far as I can make out)

Any suggestion on how to handle will be appreciated. Thanks

Note: I have already gone through several questions on SO related to interacting with hidden web elements but they differ with my situation.

解决方案

Grab the element you want to click:

# Or using xparth or something
element = driver.find_element_by_css_selector(css_selector)

Click it using javascript:

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

NOTE: I'm using jQuery otherwise select it native with javascript

这篇关于如何单击使用 Selenium WebDriver 隐藏的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
Python最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆