gpt4 book ai didi

python - Selenium关于如何在单击more_button后获取new_driver?

转载 作者:太空宇宙 更新时间:2023-11-03 16:54:30 29 4
gpt4 key购买 nike

问:我正在使用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)

最佳答案

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

这是一个想法 - a 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)

关于python - Selenium关于如何在单击more_button后获取new_driver?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35536534/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com