我希望一个善良的灵魂可以在这里帮助我。我需要点击此网站下一页的帮助:https://www.xtip.co.uk/en/today/index.html。
我的代码:
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()
它使用JavaScript和AJAX,因此我使用Selenium,但无法使其切换页面。我究竟做错了什么?通常,当我转到Chrome->检查->复制Xpath->将此路径放入find_element_by_xpath()并添加click()时,此方法正常工作。
我在控制台中的错误:
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)
答案 0 :(得分:0)
您可以使用以下代码:
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()