python - Selenium 单击instagram Python上的所有关注按钮
问题描述
当您在 Instagram 网站上单击用户的用户名时,我正在尝试自动单击 Instagram 关注按钮。
单击用户名后,您然后单击关注者并打开一个窗口,其中包含关注此人的人,并且有关注按钮
这是新窗口的屏幕截图
我试图通过python selenium 一个一个地单击按钮,但我尝试的任何方法似乎都不起作用。
我得到的最好的是一个 for 循环,它只使用 xpath 单击了第一个跟随按钮,但没有单击其他按钮。
#click the followers button to dispaly the users followers
driver.find_element_by_partial_link_text("followers").click()
time.sleep(3)
#scroll through the followers list to a specified heeight
scroll_box=driver.find_element_by_xpath("/html/body/div[4]/div/div[2]")
last_ht, ht=0, 1
while last_ht !=ht:
last_ht=ht
time.sleep(2)
ht=driver.execute_script("""arguments[0].scrollTo(0, 2000);
return 2000;
""", scroll_box)
#follow users up to the specified height
follow=driver.find_elements_by_xpath("/html/body/div[4]/div/div[2]/ul/div/li[1]/div/div[3]/button")
for x in range (0,len(follow)):
follow[x].click()
time.sleep(2)
time.sleep(1)
解决方案
您的 Xpath 选择器似乎是直接从 chrome 开发人员工具复制而来的,顺便说一句,它只会返回一个按钮,因为您的目标是一个li
# Get all buttons that has the text Follow
buttons = driver.find_elements_by_xpath("//button[contains(.,'Follow')]")
for btn in buttons:
# Use the Java script to click on follow because after the scroll down the buttons will be un clickeable unless you go to it's location
driver.execute_script("arguments[0].click();", btn)
time.sleep(2)
推荐阅读
- java - 可以从中间读取csv文件吗?
- c# - .NET Core 3.0:警告 CS8632 可空引用类型的注释只能在“#nullable”注释上下文中的代码中使用
- python - Pycharm 控制台错误 - 找不到源文件
- c# - 需要帮助以使 crc 计算的类线程安全
- python - 如何测试期望文件的点击命令
- redirect - Flutter:如何获得有关 url 重定向的通知并防止使用 WebView 对象进行重定向:
- python - 为什么我的图是分开显示的,而不是在同一张图上?
- java - 关于Processor接口的process(...)方法中定义的参数
- pandas - 枢轴非常大的表熊猫的替代品
- c# - 在 Jint 中序列化“JsValue”