1

I hope a kind soul can help me here. I need help with clicking the next page on this site: https://www.xtip.co.uk/en/today/index.html.

My code:

from selenium import webdriver

url = 'https://www.xtip.co.uk/en/today/index.html'
browser = webdriver.Chrome('./Browser/chromedriver')
browser.get(url)

amount_of_pages_xpath = browser.find_element_by_xpath('//*[@id="widget_today_tabbed"]/div[2]/div[2]/div')
amount_of_pages_html = amount_of_pages_xpath.get_attribute('data-number-of-pages')

for x in range(len(amount_of_pages_html)):
    browser.find_element_by_xpath('//*[@id="widget_today_tabbed"]/div[2]/div[2]/div/ul/li[8]/a').click()

It uses JavaScript and AJAX and therefore I use Selenium, but I can't make it switch pages. What am I doing wrong? Normally it works when I go to Chrome -> Inspect -> Copy Xpath -> Put this path into find_element_by_xpath() and add click().

enter image description here

My error in console:

  selenium.common.exceptions.WebDriverException: Message: unknown error: Element <a href="#" class="widget-page-link widget-page-link-next" data-target-page="next">...</a> is not clickable at point (917, 654). Other element would receive the click: <div class="cc_message">...</div>
          (Session info: chrome=67.0.3396.87)
          (Driver info: chromedriver=2.38.552518 (183d19265345f54ce39cbb94cf81ba5f15905011),platform=Mac OS X 10.13.5 x86_64)
jubibanna
  • 1,095
  • 9
  • 21

1 Answers1

0

You can use this code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 

browser = webdriver.Chrome(executable_path = r'D:/Automation/chromedriver.exe')
browser.get("https://www.xtip.co.uk/en/today/index.html")

wait = WebDriverWait(browser, 30)

browser.execute_script("window.scrollTo(0, 800)") 

next_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.widget-page-link.widget-page-link-next")))

next_button.click()
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • Thank you so much, it works! Can you explain why you need browser.execute_script("window.scrollTo(0, 800)") and why you use CSS_SELECTER and not xpath? Thanks! – jubibanna Jun 24 '18 at 16:45
  • You have to tell your driver to scroll down so that the drive will be able to interact with the desire element. – cruisepandey Jun 24 '18 at 16:46
  • Css selector perform far better than xpath. I’ll share the link – cruisepandey Jun 24 '18 at 16:48
  • https://stackoverflow.com/questions/16788310/what-is-the-difference-between-css-selector-xpath-which-is-betteraccording-t – cruisepandey Jun 24 '18 at 16:50