east east - 4 years ago 182
Python Question

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!

Answer Source

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()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download