Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

python - How do you click on an element which is hidden using Selenium WebDriver?

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

The issue is that there is a menu something like this enter image description here 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...