Python Selenium 无法单击() 按钮

标签 python selenium-webdriver

我正在尝试从这里自动抓取链接:

https://thegoodpubguide.co.uk/pubs/?paged=1&order_by=category&search=pubs&pub_name=&postal_code=&region=london

一旦我有了第一页,我想单击底部的右侧 V 形图标,以便移动到第二页、第三页,依此类推。抓取之间的链接。

不幸的是,我尝试的任何方法都不允许我将 chrome 发送到下一页。

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    import time
    from datetime import datetime
    import csv
    from selenium.webdriver.common.action_chains import ActionChains

    #User login info
    pagenum = 1

    #Creates link to Chrome Driver and shortens this to 'browser'
    path_to_chromedriver = '/Users/abc/Downloads/chromedriver 2' # change path as needed
    driver = webdriver.Chrome(executable_path = path_to_chromedriver)

    #Navigates Chrome to the specified page
    url = 'https://thegoodpubguide.co.uk/pubs/?paged=1&order_by=category&search=pubs&pub_name=&postal_code=&region=london'


    #Clicks Login
    def findlinks(address):

        global pagenum

        list = []

        driver.get(address)
        #wait
        while pagenum <= 2:

            for i in range(20): # Scrapes available links
                xref = '//*[@id="search-results"]/div[1]/div[' + str(i+1) + ']/div/div/div[2]/div[1]/p/a'
                link = driver.find_element_by_xpath(xref).get_attribute('href')
                print(link)
                list.append(link)

            with open("links.csv", "a") as fp: # Saves list to file 
                wr = csv.writer(fp, dialect='excel')
                wr.writerow(list)

            print(pagenum)

            pagenum = pagenum + 1

            element = driver.find_element_by_xpath('//*[@id="search-results"]/div[2]/div/div/ul/li[8]/a')
            element.click()


    findlinks(url)

有什么东西挡住了我没有看到的按钮吗?

我的终端中打印的错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="search-results"]/div[2]/div/div/ul/li[8]/a"}

最佳答案

试试这个:

element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[class='next-page btn']"))  
element.click()

关于Python Selenium 无法单击() 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50354465/

相关文章:

python - PyPdf合并错误

python - 在 Eclipse 中为 PEP8 设置最大行长度问题

python - 如何为新标签设置页面加载超时

java - 获取表大小时如何排除表的第一行和最后一行

python公式从另一张表中获取值与python有空间

python - botocore.exceptions.ClientError : An error occurred (AccessDenied) when calling the PutObject operation: Access Denied

python - 替换列表元素中的空格

selenium - 使用 XPath 在 Selenium 中查找元素

java - 鼠标悬停后点击

python - Selenium webdriver用python来抓取动态页面找不到元素