【问题标题】:Click load more button jooble selenium python单击加载更多按钮jooble selenium python
【发布时间】:2021-12-08 19:15:40
【问题描述】:

我正在尝试通过网络抓取此page,并且我正在寻找一种使用 selenium python 单击加载更多按钮的方法。我试过这些代码

driver.find_element(By.LINK_TEXT, "Load more").click()

driver.find_element_by_xpath('//*[@id="root"]/div/div[1]/div[1]/main/div[2]/div[1]/div/button/span').click()

driver.find_element_by_xpath('//*[@id="root"]/div/div[1]/div[1]/main/div[2]/div[1]/div/button').click()

但以上都没有工作的主要代码,我的替代解决方案是使用这样的滚动......

def infinite(driver):
    
    scroll_pause_time = 10

    # Get scroll height
    last_height = driver.execute_script("return document.body.scrollHeight")

    while True:

        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

        time.sleep(scroll_pause_time)

        try:
            driver.find_element_by_xpath('//*[@id="root"]/div/div[1]/div[1]/main/div[2]/div[1]/div/button').click()#This doesn't work
        except:
            print('No button')
        new_height = driver.execute_script("return document.body.scrollHeight")
        driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.HOME)
        print('yeah!')
        if new_height == last_height:
            # If heights are the same it will exit the function
            break
        last_height = new_height

【问题讨论】:

    标签: python selenium selenium-webdriver web-scraping beautifulsoup


    【解决方案1】:

    有一个modal pop-up window,您可以点击是或否或X按钮。

    一旦是clicked,你必须一直向下滚动到按钮,Selenium 才能看到加载更多按钮,最终你可以点击它。

    代码:

    driver = webdriver.Chrome(driver_path)
    driver.maximize_window()
    wait = WebDriverWait(driver, 30)
    
    driver.get("https://jooble.org/SearchResult?date=8&p=5&rgns=New%20York%2C%20NY&ukw=software")
    try:
        wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='Close']"))).click()
    except:
        pass
    
    driver.execute_script("var scrollingElement = (document.scrollingElement || document.body);scrollingElement.scrollTop = scrollingElement.scrollHeight;")
    wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Load more']/.."))).click()
    

    进口:

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

    【讨论】:

      猜你喜欢
      • 2022-12-18
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      • 1970-01-01
      • 2021-03-18
      • 1970-01-01
      • 2015-08-10
      • 2020-07-28
      相关资源
      最近更新 更多