Selenium点击一个按钮

时间:2017-05-10 20:16:45

标签: jquery selenium

我正在尝试使用Selenium单击带有以下HTML代码的按钮:

<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>

我可以使用以下代码找到此按钮:

element = driver.find_element_by_id("ctl00_ctl00_cphMain_main_ucSearchResult_rptPager_ctl01_btnPageNumber")

但如果我这样做:

element.click()

我收到一条错误消息,即:

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)

我正在尝试浏览的网址是:

https://en.camping.info/campsites

2 个答案:

答案 0 :(得分:2)

有两个div元素与分页面板重叠并接收点击。您可以使用div删除那些JavaScriptExecutor元素(隐藏它们以便能够点击所需按钮),如下所示:

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()

答案 1 :(得分:2)

尝试这样的事情:

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()

这里的问题是3个案例中的任何一个

  1. 元素不可见或显示
  2. 元素上面有覆盖
  3. 在页面刷新后生成元素(意味着它在几秒钟内不可见,然后显示)
  4. Great StackOverflow解释此

    HERE