无法周期性地单击不同类别以到达目标页面

时间:2018-08-27 08:53:31

标签: python python-3.x selenium selenium-webdriver web-scraping

我用python与硒结合编写了一个脚本,以单击几个类别以到达网站的目标页面。我的下方脚本可以执行一次相同的操作,但是重复执行该过程时,会引发stale element错误。我如何使它成功?

这是我的尝试:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

url = "https://www.courts.com.sg/"

def get_information(driver,mlink):
    driver.get(mlink)
    #the following line click on the menu
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'span.nav-toggle'))).click()
    # It now clicks on the individual categories
    for item in wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,'#menu-top1535320854159022796-menu .nav-anchor .opener'))):
        item.click()
        # It then click on the sub-categories
        for link in wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,'.nav-dropdown h3 a'))):
            link.click()
            # Then on the target items
            for ilink in wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,'li.show a')))[1:]:
                ilink.click()
                #this is where the scripts throws "stale element error" never gets back to repeat the process


if __name__ == '__main__':
    driver = webdriver.Chrome()
    wait = WebDriverWait(driver,10)
    try:
        get_information(driver,url)
    finally:  
        driver.quit()

2 个答案:

答案 0 :(得分:1)

以下是解决问题的另一种方法...(我不链接CSS选择器:))

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
from bs4 import BeautifulSoup

url_list = []
url = "https://www.courts.com.sg/"
def get_information(driver,mlink):
    driver.get(mlink)
    sleep(5)
    soup = BeautifulSoup(driver.page_source)
    uls = soup.find_all('ul', {'class': 'nav-mobile'})
    for li in uls[0].find_all('li', {'class': 'nav-item'}):
        submenu = li.find_all('div', {'class':'nav-dropdown'})
        uls = submenu[0].find_all('ul')
        for ul in uls:
            all_li = ul.find_all('li')
            for i in range(1,len(all_li)):
                a = all_li[i].find_all('a')
                print(a)
                a = 'https://www.courts.com.sg' + a[0].get('href')
                url_list.append(a)

if __name__ == '__main__':
    driver = webdriver.Chrome('C:/Users/sarthak_negi_/Downloads/chromedriver_win32/chromedriver.exe')
    wait = WebDriverWait(driver,10)
    try:
        get_information(driver,url)
    finally:  
        driver.quit()

url_list包含下拉选项的所有链接。进行访问即可访问该页面。希望这会有所帮助!

答案 1 :(得分:1)

之所以会得到stale element,是因为当您将鼠标悬停在菜单上时,菜单会打开,而在循环中单击则会消失。
解决方案1-在不打开菜单的情况下获取所有子菜单链接:

def get_information(driver,mlink):
    driver.get(mlink)
    submenu_links = []
    submenus = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,'.submenu li a')))
    for submenu in submenus:
        submenu_links.append(submenu.get_attribute("href"))

    for link in submenu_links
        driver.get(link)

解决方案2-如果必须打开菜单:

def get_information(driver,mlink):
    driver.get(mlink)
    #the following line click on the menu, what is this and why?
    #wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'span.nav-toggle'))).click()
    # It now clicks on the individual categories

    menus = wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,'.navigation li.nav-item')))
    for menu in menus:

        ActionChains(driver).move_to_element(menu).perform()

        submenu_links = []
        submenus = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,'.submenu li a')))
        for submenu in submenus:
            submenu_links.append(submenu.get_attribute("href"))

        for link in submenu_links
            driver.get(link)