Python:单击下一页,没有链接可跟踪(javascript/AJAX)

2024-09-26 18:12:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望有一个善良的人能在这里帮助我。我需要帮助单击此网站上的下一页: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()

所以我不能使用Selenium页面来切换。我做错什么了?通常,当我转到Chrome->;Inspect->;Copy Xpath->;将此路径放入find_element_by_Xpath()和add click()时,它正常工作。在

enter image description here

我在控制台中的错误:

^{pr2}$

Tags: ofhttpsgtdivbrowsertodaybyhtml
1条回答
网友
1楼 · 发布于 2024-09-26 18:12:40

您可以使用以下代码:

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

相关问题 更多 >

    热门问题