26

I have a grid which displays some records. When I click on a record and inspect that element it is shown that it is hidden but it is visible in the grid.

My HTML is:

<a href="http://192.168.1.6/eprint_prod_3.8/settings/othercost_add.aspx?type=edit&id=805" title="Plastic Spiral Bind"
<div style="float: left; width: 99%; overflow: hidden; height: 15px; overflow: hidden"> Plastic Spiral Bind </div>
</a>

The above code is hidden while inspecting but it is visible in grid.

Selenium code:

driver.findElement(By.partialLinkText("Plastic Spiral Bind")).click();
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Santhosh Siddappa
  • 708
  • 3
  • 14
  • 29
  • 3
    Seems to be an FAQ item : http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions#Q:_Why_is_it_not_possible_to_interact_with_hidden_elements? You would require javascript. – Jayan Mar 01 '14 at 05:00
  • @Santhosh.S - What is the issue you are facing ? Is the .click() not working? – StrikerVillain Mar 01 '14 at 09:06

7 Answers7

35

First store that element in object, let's say element and then write following code to click on that hidden element:

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);
Alpha
  • 13,320
  • 27
  • 96
  • 163
  • 2
    I don't understand why Selenium believes that my Save button isn't visible, but you saved (pun intended) me with this snippet. – Brook Mar 16 '18 at 16:15
  • Thanks! I can able to click the element even if it is not visible on screen. In my case it's a slide type. The button is not visible but present in DOM. (isElementDisplayed() always returns true in this scenario). – ARods Jun 18 '19 at 12:14
  • is there a way to simulate Double Click ? I tried click twice but it's not working – Meir Tolpin Jul 30 '19 at 10:31
14

You have two approaches. Selenium has been specifically written to NOT allow interaction with hidden elements. The rational is that if a person cannot perform that action, then neither should Selenium. Therefore, to perform the click via Selenium, you must perform the action a user would do to make that button visible (e.g mouse over event, click another element, etc) then perform the click once visible.

However, Selenium does allow you to execute Javascript within the context of an element, so you could write Javascript to perform the click event even if it is hidden.

My preference is to always try and perform the actions to make the button visible

Robbie Wareham
  • 3,380
  • 1
  • 20
  • 38
  • 1
    `overflow: hidden` doesn't neccesrely mean that it is hidden. – Erki M. Mar 02 '14 at 23:21
  • Using pseudo elements to create custom styled radios and checkboxes is another reason you need to click a hidden element. – ESR Mar 06 '17 at 11:40
5

Here is the script in Python.

You cannot click on elements in selenium that are hidden. However, you can execute JavaScript to click on the hidden element for you.

element = driver.find_element_by_id(buttonID)
driver.execute_script("$(arguments[0]).click();", element)
Peter
  • 202
  • 2
  • 5
0
overflow:hidden 

does not always mean that the element is hidden or non existent in the DOM, it means that the overflowing chars that do not fit in the element are being trimmed. Basically it means that do not show scrollbar even if it should be showed, so in your case the link with text

Plastic Spiral Bind

could possibly be shown as "Plastic Spir..." or similar. So it is possible, that this linkText indeed is non existent.

So you can probably try:

driver.findElement(By.partialLinkText("Plastic ")).click();

or xpath:

//a[contains(@title, \"Plastic Spiral Bind\")]
Erki M.
  • 5,022
  • 1
  • 48
  • 74
0

I did it with jQuery:

page.execute_script %Q{ $('#some_id').prop('checked', true) }
aarkerio
  • 2,183
  • 2
  • 20
  • 34
-1

If the <div> has id or name then you can use find_element_by_id or find_element_by_name

You can also try with class name, css and xpath

find_element_by_class_name
find_element_by_css_selector
find_element_by_xpath
user2118784
  • 442
  • 1
  • 6
  • 18
-5

Use an XPath of the link by using Selenium IDE to click the element:

driver.findelement(By.xpath("//a[contains(@title, \"Plastic Spiral Bind\")]")).click();
honk
  • 9,137
  • 11
  • 75
  • 83