javascript 如何使用Selenium在python中单击分页页码?

7rtdyuoh  于 2023-04-28  发布在  Java
关注(0)|答案(1)|浏览(93)

几天来,我一直在努力弄清楚如何点击分页类的页码。我尝试了很多方法,但我只能突出显示数字的容器,而不是真正点击它。

这是网站中分页的源代码:

<nav aria-label="pagination">
    <ul class="pagination">
        <li class="page-item false" data-page-number="<-">
            <a class="page-link" id="page-link«" href="#">«</a>
        </li>
        <li class="page-item active" data-page-number="1">
            <a class="page-link" id="page-link1" href="#">1</a>
        </li>
        <li class="page-item " data-page-number="2">
            <a class="page-link" id="page-link2" href="#">2</a>
        </li>
        <li class="page-item " data-page-number="3">
            <a class="page-link" id="page-link3" href="#">3</a>
        </li>
        <li class="page-item " data-page-number="4">
            <a class="page-link" id="page-link4" href="#">4</a>
        </li>
        <li class="page-item " data-page-number="5">
            <a class="page-link" id="page-link5" href="#">5</a>
        </li>
        <li class="page-item false" data-page-number="->">
            <a class="page-link" id="page-link»" href="#">»</a>
        </li>
    </ul>
</nav>

这是我用来定位和单击页码的当前代码:

try:
    WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "ul.pagination")))
    next_page_number = page_number + 1
    driver.find_element(By.XPATH, f'//*[@id="page-link{next_page_number}"]').click()
    time.sleep(2)

except NoSuchElementException or TimeoutException:
    break

我也试着替换了。click()使用driver.execute_script("arguments[0].click();", page)的不同执行方法,但它并没有更好地工作。
driver.find_element(By.XPATH, f"//a[@href='#'][text()='{next_page_number}']").click()-没有区别。
driver.find_element(By.CSS_SELECTOR, "ul.pagination").find_element(By.LINK_TEXT, f"{next_page_number}").click() - ;-;
我也试过用css选择器来查找这个号码,但是也没有用。我也尝试过用send_keys(Keys.ENTER)代替click()
总的来说,我很失落,所以任何帮助或指导都将非常感激。
编辑:我正在查看的具体网站是https://www.xior-booking.com

sgtfey8w

sgtfey8w1#

我已经更新了nextpage定位器,你要点击如下目标与链接元素

//li[@data-page-number="{next_page_number}"]//a

然后使用javscript click来单击链接

driver.execute_script("arguments[0].click();", nextPageLink)

完整代码

from selenium.webdriver.support import expected_conditions as EC
    import undetected_chromedriver as uc

    driver = uc.Chrome()
    driver.get('https://www.xior-booking.com/#')

try:
    WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "ul.pagination")))
    WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//li[@class="page-item '
                                                                                'active"]//a')))
    # first we get the current Active page number
    currentPageNumber = driver.find_element(By.XPATH, '//li[@class="page-item active"]//a').text
    # Then we calculate the next page number
    next_page_number = int(currentPageNumber) + 1
    nextPageLink = driver.find_element(By.XPATH, f'//li[@data-page-number="{next_page_number}"]//a')

    # doing a javascript click instead fo element.click
    driver.execute_script("arguments[0].click();", nextPageLink)
    # Increased wait here to visually verify
    time.sleep(20)

except NoSuchElementException or TimeoutException:
    print("exception Occurred")

点击第2页链接

相关问题