Selenium push button
I am trying to use Selenium to click a button with the following HTML code:
<a id="ctl00_ctl00_cphMain_main_ucSearchResult_rptPager_ctl01_btnPageNumber" class="pager2"href="javascript:__doPostBack('ctl00$ctl00$cphMain$main$ucSearchResult$rptPager$ctl01$btnPageNumber','')">2</a>
I can find this button using the following code:
element = driver.find_element_by_id("ctl00_ctl00_cphMain_main_ucSearchResult_rptPager_ctl01_btnPageNumber")
but if I then do:
element.click()
I am getting an error, namely:
WebDriverException: Message: unknown error: Element <a id="ctl00_ctl00_cphMain_main_ucSearchResult_rptPager_ctl01_btnPageNumber" class="pager..." href="javascript:__doPostBack('ctl00$ctl00$cphMain$main$ucSearchResult$rptPager$ctl01$btnPageNumber','')">2</a> is not clickable at point (82, 516). Other element would receive the click: <p>...</p>
(Session info: chrome=57.0.2987.133)
(Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 10.0.10586 x86_64)
The url I am trying to navigate to is:
https://en.camping.info/campsites
+3
source to share
2 answers
There are two elements div
that overlap the pagination bar and get clicked. You can get rid of these elements div
(hide them to click on the desired button) using JavaScriptExecutor
as shown below:
driver.get("en.camping.info/campsites")
page = driver.find_element_by_id("ctl00_ctl00_cphMain_main_ucSearchResult_rptPager_ctl01_btnPageNumber")
nav_div = driver.find_element_by_id('jq-app-buttons-wrapper')
driver.execute_script('arguments[0].style.display="none";', nav_div)
cookies_div = driver.find_element_by_id('cookie-consent-wrapper')
driver.execute_script('arguments[0].style.display="none";', cookies_div)
page.click()
+2
source to share
Try something like this:
from selenium.webdriver.common.action_chains import ActionChains
elem = driver.find_element_by_xpath('''//*[@id="ctl00_ctl00_cphMain_main_ucSearchResult_rptPager_ctl00_btnPageNumber"]''')
#I've used xpath here instead of id you can change that.
ActionChains(driver).move_to_element(elem).click().perform()
The problem here is that either one of the 3 cases
- Item is not visible or displayed
- The element has a caption over it
- The element is created after the page has been refreshed (this means it is not displayed for a few seconds and then displayed)
Great StackOverflow Explanation of this
HERE
+1
source to share