Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium to click a 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 by 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 get an error message, 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'm trying to navigate through is:

https://en.camping.info/campsites
like image 588
HolyMonk Avatar asked Apr 06 '26 19:04

HolyMonk


2 Answers

There are 2 div elements that overlaps the pagination panel and receives the click. You might get rid of those div elements (hide them to be able to click on required button) with JavaScriptExecutor as 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()
like image 123
Andersson Avatar answered Apr 09 '26 10:04

Andersson


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 issue here is that either One of 3 cases

  1. Element is not visible or shown
  2. Element has an overlay over it
  3. Element is generated after the page is refreshed (Meaning it's not visible for a few seconds then it shows)

Great StackOverflow Explaining this

HERE


like image 35
innicoder Avatar answered Apr 09 '26 10:04

innicoder