向下滚动,直到在Python中找到元素 [英] Scroll down until elements are found in Python

查看:233
本文介绍了向下滚动,直到在Python中找到元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上我想创建一个Facebook脚本,该脚本将添加我的朋友朋友列表"中的所有朋友,现在的问题是我们已经有很多共同的朋友(显示在朋友"的顶部列表"),我想一直向下滚动直到出现添加朋友",一旦出现,我希望脚本单击该按钮并向他添加不是我朋友的其他所有人.现在,我对编程还很陌生,所以如果您可以详细地向我解释如何执行此PS,我知道应该在Java中完成,但是有人可以向我解释如何在Python中做到这一点吗?

So basically i want to make a Facebook script that is going to add all friends from my friends "Friend List", now the problem is we already have a lot of mutual friends (that are shown on the top of the "Friend List"), I want to scroll all the way down until "Add Friend" appears, and once it appears I want the script to click the button and add him everyone else that is not my friend. Now I am pretty new to programming so if you can to explain me more in details how to do this PS: i know that this should be done in Java but can someone explain me how to do this in Python?

这是代码:

root.get(targets_url)        #targets_url is the link of targets friend list
time.sleep(10)

while True:
    element = root.find_element_by_class_name('FriendRequestAdd').click()

推荐答案

这不一定要用Java完成,python可以正常工作.您不必滚动到add friends出现的位置,可以使用find_element_by_id或任何其他查找元素的方法,它仍然可以找到它.但是,如果您确实有心去寻找并滚动到该元素.

This does not have to be done in Java, python will work just fine. You do not necessarily have to scroll to where add friends appears, you can use the find_element_by_id or any other method of finding elements and it will still locate it. But if you truely have your heart set on finding and scrolling to the element.

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

wait = WebDriverWait(root)     

root.wait(3) #wait for element to visible. implicit wait.

#feel free to change XPATH to CSS_SELECTOR or whatever suits you
friendlist = wait.until(ec.element_to_be_clickable((By.XPATH,'xpath value'))  #explicitly wait for element to appear on the page
root.execute_script("arguments[0].ScrollIntoView;", friendlist)
friendlist.click()

等待是为了确保add friend id在硒之前加载 假设它没有随页面一起加载,便开始搜索它 立即地.如果硒仍然看不到add friend,那么我会 建议您使用expected_conditionWebDriverWait, 我完全怀疑它是否可以解决这个问题

The wait is to ensure that the add friend id loads before selenium starts searching for it, assuming it doesn't load with the page immediately. If selenium still doesn't see add friend then I would recommend you make use of expected_condition and WebDriverWait , altough I very much doubt it will get to this

如果显式等待不起作用,则应检查页面的该部分是否存在于iframe或其他框架上

If an explicit wait does not work, then you should check if that portion of the page exists on an iframe or a different frame

这篇关于向下滚动,直到在Python中找到元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆