【发布时间】:2021-04-01 07:49:44
【问题描述】:
我正在解析一些带有<div class="classname" ...> tgs 列表的网上商店页面。例如,每页 24 个。
但是有些元素及时加载,有些没有。 WebDriver(Chrome) find 需要 4-6 个元素,通常像这样加载:
<div class="classname">
<div class="abcd">...</div>
</div>
和 18-20 喜欢
<div class="classname" ...><!-- --></div> - 未加载
所以我使用driver.find_elements_by_class("abcd"),它只有 4-6 个元素。
如何等待使用WebDriverWait.until 或implicity_wait 加载的总列表?
(没有任何其他元素,可以等待。页面的所有其他部分完全正确加载。)
或者如何在没有条件的情况下简单地延迟几秒钟并在 WebDriver 对象中获取页面的完成版本? (driver.iImplicity_wait(10)) - 如我所见,延迟,但 webdriver 对象中的数据也不完整。)
更新: 这对我来说很奇怪,但是使用 webdriver.wait、time.sleep()、drver.refresh() 不会更新页面的 drive.page_source。那仍然停留在未正确加载的语句中... 代码:
self.driver.get(url_)
time.sleep(15)
number_of_elements = len(self.driver.find_elements_by_class_name("product-cards-layout__item")) # len -24
while True:
xpath = "//div[@class=\"product-card--mobile\"]"
condition = EC.presence_of_all_elements_located((By.XPATH, xpath))
try:
wait = WebDriverWait(self.driver, 10).until(condition) # len - 6
except Exception:
pass
print(len(wait)) #6
if len(wait) == number_of_elements:
break
else:
self.driver.refresh()
exit_ = self.driver.page_source
所以。 driver.page_sorce 中的 html 代码如下:
<div class="product-cards-layout__item"><div class="product-card--mobile__info"</div></div>
<div class="product-cards-layout__item"><div class="product-card--mobile__info"</div></div>
<div class="product-cards-layout__item"><div class="product-card--mobile__info"</div></div>
... (6 times)
<div class="product-cards-layout__item"><!-- --></div>
<div class="product-cards-layout__item"><!-- --></div>
<div class="product-cards-layout__item"><!-- --></div>
... (20 times)
Total 24 TAGS
但在 Chrome 打开的窗口中,我看到了所有需要的信息(24 个完整的标签,在构造中
【问题讨论】:
-
具有这种行为的网站会有所帮助。页面上可能存在帮助器,例如要检查的加载微调器,直到它不存在为止。
标签: python selenium parsing selenium-webdriver webdriverwait