python - Python & Selenium:无法定位和点击 Facebook 的点赞和分享按钮
问题描述
我正在尝试使用 xpath 在 facebook 页面上找到“Like”和“Share”按钮,但是在 facebook 页面加载后,python 脚本执行完成并且没有单击 like 按钮。
我从 facebook 页面获得了点赞按钮 xpath。
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
def connect():
driver.set_page_load_timeout("10")
driver.get("https://www.facebook.com/mytheoryofevolution/")
time.sleep(4)
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
driver = webdriver.Chrome(executable_path=r"C:\Users\javed\PycharmProjects\click-website\drivers\chromedriver.exe",chrome_options=chrome_options)
connect()
like_buttons = driver.find_elements_by_xpath('//a[contains(@class,"likeButton")]')
for like in like_buttons:
print(like)
break
解决方案
t你不需要
break
因为当它开始 for 循环时,您将获得第一项,然后循环将中断
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
driver = webdriver.Chrome(executable_path=r"C:\Users\javed\PycharmProjects\click-website\drivers\chromedriver.exe",chrome_options=chrome_options)
def connect():
driver.set_page_load_timeout("10")
driver.get("https://www.facebook.com/mytheoryofevolution/")
time.sleep(4)
like_buttons = driver.find_elements_by_xpath('//a[contains(@class,"likeButton")]')
for like in like_buttons:
print(like)
connect()
推荐阅读
- python - 如何将 xml 节点作为数据框的列?
- python - 将我的 kivy 代码打包到 android 应用程序时出错
- swiftui - 所有 SwiftUI 视图都是 UIKit 视图的中介吗?
- python - 文本排名在 0 和 1 之间归一化
- android - 调试时应用程序卡在初始屏幕上 - Xamarin Android
- python - 在 python 中为 anova 运行 R 代码时出错
- command-line - 如何卸载使用 electron-builder 生成的 exe
- c# - Winform在运行时更改数据集的连接字符串而不使用exe.config
- bitnami - 用于创建存储库的 phabricator curl 命令行
- ethereum - Solidity:我应该使用哪种方法来调用外部合约的函数?