滚动直到屏幕上出现按钮

时间:2020-10-24 07:23:51

标签: python selenium

我看过有关等待元素出现的类似文章,但找不到与我的问题有关的任何内容。现在我有向下滚动的部分,我想在显示屏幕上显示按钮之后单击它,不仅要等待元素出现,还要让用户一次在屏幕上看到按钮它滚动到它。

我尝试使用is_displayed()是因为我认为一旦按钮上的元素显示在屏幕上,它将返回True,但这似乎不是我要找的电话。

我还尝试提取fill属性以检查十六进制代码,但似乎无法识别它。

for i in range(1000):
    driver.execute_script("window.scrollBy(0,1))
    # print(driver.find_element_by_class_name("QBdPU")).is_displayed()
    # print(driver.find_element_by_class_name("QBdPU")).get_attribute("fill")

这是页面中的HTML代码

<button class="wpO6b " type="button">
<div class="QBdPU ">
   <span class="">
      <svg aria-label="Like" class="_8-yf5 " fill="#262626" height="24" 
       viewBox="0 0 48 48" width="24"><path d="M34.6 6.1c5.7 0 10.4 5.2 10.4 11.5 
       0 6.8-5.9 11-11.5 16S25 41.3 24 41.9c-1.1-.7-4.7-4-9.5-8.3-5.7-5-11.5-9.2- 
       11.5-16C3 11.3 7.7 6.1 13.4 6.1c4.2 0 6.5 2 8.1 4.3 1.9 2.6 2.2 3.9 2.5 
       3.9.3 0 .6-1.3 2.5-3.9 1.6-2.3 3.9-4.3 8.1-4.3m0-3c-4.5 0-7.9 1.8-10.6 
       5.6-2.7-3.7-6.1-5.5-10.6-5.5C6 3.1 0 9.6 0 17.6c0 7.3 5.4 12 10.6 16.5.6.5 
       1.3 1.1 1.9 1.7l2.3 2c4.4 3.9 6.6 5.9 7.6 6.5.5.3 1.1.5 1.6.5.6 0 1.1-.2 
       1.6-.5 1-.6 2.8-2.2 7.8-6.8l2-1.8c.7-.6 1.3-1.2 2-1.7C42.7 29.6 48 25 48 
       17.6c0-8-6-14.5-13.4-14.5z"></path>
      </svg> 
   </span>
</div>
</div>

任何关于前进的建议都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

按钮出现在页面上后,您可以使用以下代码将其滚动到视图中:

    button = driver.find_element_by_class_name(class_button)
    driver.execute_script("arguments[0].scrollIntoView();", button)