关于如何在点击more_button后获取new_driver的Selenium?

时间:2016-02-21 13:08:12

标签: python selenium selenium-webdriver

问: 我使用Selenium获取包含内容的页面,点击“更多”按钮后,页面会输出更多内容,以及如何通过webdriver获取新页面?

这样的一些代码:

def parase_questions(self):
    driver = self.login()
    driver.implicitly_wait(2)
    more_btn = driver.find_element_by_css_selector(".zg-btn-white.zg-r3px.zu-button-more")
    more_btn.click()
    # should I do something to get the new driver ?
    print driver.page_source
    question_links = driver.find_elements_by_css_selector('.question_link')
    print len(question_links)

1 个答案:

答案 0 :(得分:1)

如果我理解正确,单击“更多”按钮后,会有更多元素加载question_link类。您需要一种方法来等待加载问题链接。

这是一个想法 - custom Expected Condition可帮助您等到N个元素数超过

from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.support import expected_conditions as EC

class wait_for_more_than_n_elements(object):
    def __init__(self, locator, count):
        self.locator = locator
        self.count = count

    def __call__(self, driver):
        try:
            count = len(EC._find_elements(driver, self.locator))
            return count > self.count
        except StaleElementReferenceException:
            return False

用法:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

driver = self.login()
driver.implicitly_wait(2)

question_links = driver.find_elements_by_css_selector('.question_link')

more_btn = driver.find_element_by_css_selector(".zg-btn-white.zg-r3px.zu-button-more")
more_btn.click()

# wait
wait = WebdriverWait(driver, 10)
wait.until(wait_for_more_than_n_elements((By.CSS_SELECTOR, ".question_link"), len(question_links))

# now more question links were loaded, get the page source
print(driver.page_source)