【问题标题】:Python Selenium unable to click() buttonPython Selenium 无法单击()按钮
【发布时间】:2018-05-15 15:52:18
【问题描述】:

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

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"}

【问题讨论】:

    标签: python selenium-webdriver


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 添加所需的导入后,我仍然得到:selenium.common.exceptions.WebDriverException: Message: unknown error: Element is not clickable at point (565, 4990)
    • 是的,这个错误很好!您需要向下滚动以单击下一个箭头。向下滚动,然后使用我提供的代码。
    • 我已经检查了我提供的 css 选择器是通用的。如果执行向下滚动,它将起作用。
    • 知道了!效果很好。我只包括: driver.execute_script("window.scrollTo(0, 5000);") 以便我可以自动滚动。它确实抛出了 selenium.common.exceptions.WebDriverException: Message: unknown error: call function result missing 'value' ,但我需要做的就是下载更新的 chrome 驱动程序,一切正常。
    【解决方案2】:

    编辑:

    您为 V 形指定的 xpath 在页面之间是可变的,并且不完全正确。注意li[6]li[8]li[9]

    在第 1 页:xpath 是 //*[@id="search-results"]/div[2]/div/div/ul/li[6]/a/i

    在第 2 页:xpath 是 //*[@id="search-results"]/div[2]/div/div/ul/li[8]/a/i

    在第 3 页:xpath 是 //*[@id="search-results"]/div[2]/div/div/ul/li[9]/a/i

    您必须想出一些方法来确定要使用的 xpath。这里有一个提示:似乎//*[@id="search-results"]/div[2]/div/div/ul/ 下的最后一个li 表示雪佛龙。


    原帖:

    您可能想尝试等待页面加载,然后再尝试查找并单击 V 形。当我测试我的自动化脚本时,我通常只做一个time.sleep(...),但对于(可能)更复杂的功能,试试Waits。请参阅文档here

    【讨论】:

    • 似乎没有什么不同。问题似乎是它首先无法找到按钮。无论如何,我都添加了等待。谢谢。
    • 在这里添加我的 .02,之前在 selenium 中的隐式等待救了我。
    • @musikreck 感谢您的编辑,我一定会看一看。但是我认为我仍然缺少一些东西,因为我无法点击 p1 的按钮。
    猜你喜欢
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多