east
east

Reputation: 35

Python+Selenium Scrape Error: ElementNotVisibleException

I'm currently trying to use Selenium/Python/PhantomJS to scrape the results of the form below:

http://gis.vgsi.com/newhavenct/Sales.aspx

It looks like when I try to click on the search button, I get an ElementNotVisibleException error.

My code:

self.driver.find_element_by_id("MainContent_btnSearch").click()

After some digging online, it seems like the button may be hidden. Indeed, here is the relevant HTML code from the search page:

<input type="button" value="Search!" class="btn btn-primary searchTrigger" style="width: 200px;" />
<input type="submit" name="ctl00$MainContent$btnSearch" value="Search" id="MainContent_btnSearch" style="width: 200px; display: none;" />
<div id="MainContent_ctl00" style="display:none;">
</div>

I tried preceding my previous code with a click of the searchTrigger, but this is still not working:

self.driver.find_element_by_class_name("searchTrigger").click()
self.driver.find_element_by_id("MainContent_btnSearch").click()

Any advice would be greatly appreciated!

Upvotes: 0

Views: 142

Answers (2)

Andersson
Andersson

Reputation: 52675

Requested element has attribute style="display:none;", so you need to make it visible

Try to use following code:

self.driver.execute_script('document.getElementById("MainContent_btnSearch").style.display="block";')
self.driver.find_element_by_id("MainContent_btnSearch").click()

Upvotes: 1

JulienD
JulienD

Reputation: 7293

A portion of the screen is not visible by Selenium. It may be that another element lies on top of the one you are trying to click.

For me what always works without spending hours searching for the cause is to click using Javascript:

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

where elt is a WebElement - e.g. returned by find_element_by_id(.). Make it a function and use wherever the problem occurs.

Edit: it is a generic answer for when it happens again, but in this particular case the other answer is probably correct.

Upvotes: 1

Related Questions