使用硒登录Facebook [英] Logging Facebook using selenium

查看:83
本文介绍了使用硒登录Facebook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这不是一个适当的技术问题,但是在使用硒制作Facebook发布机器人时,我遇到了问题. 到目前为止,这是我的代码

I know this is a not a proper technical question, but i am facing problem while using selenium to make a facebook post bot. This is my code so far

    from selenium import webdriver

browser = webdriver.Firefox(executable_path='D:\\soft\\geckodriver-v0.18.0-win64\\geckodriver.exe')
browser.get('http://www.facebook.com')
emailElem = browser.find_element_by_id('email')
passElem = browser.find_element_by_id('pass')
submitIt = browser.find_element_by_id('u_0_r')
email_id = 'xxxxxxxxxxxxxx'
password = 'xxxxxxx'
status = 'Hie all'
emailElem.send_keys(email_id)
passElem.send_keys(password)
submitIt.click()
statusBox = browser.find_element_by_xpath(
   "//*[@id='js_kk']/div[1]/div/div[1]/div[2]/div/div/div/div/div/div[2]/div/div/div/div/span/br")
statusBox.send_keys("Hie")

现在,问题是我的代码无法在Fb页面中找到状态框,我对Selenium还是很陌生.任何帮助都会很好.它是用python编码的.

Now, the problem is my code is not able to locate the status box in Fb page, I am quite new to selenium. Any help would be nice.It is coded in python.

推荐答案

这是访问 Facebook Login Page ,通过一组有效的凭据登录,并使用xpath Status Box 中键入"Hie" 以及css_selector:

Here is the sample code block to access the Facebook Login Page, login through a valid set of credentials and type in "Hie" in the Status Box using xpath as well as css_selector:

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

driver = webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("https://www.facebook.com/")
driver.find_element_by_xpath("//input[@id='email']").send_keys("email@domain.com")
driver.find_element_by_xpath("//input[@id='pass']").send_keys("password")
driver.find_element_by_xpath("//input[starts-with(@id, 'u_0_')][@value='Log In']").click()
print(driver.title)
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(@id, 'u_0_')]//textarea[@name='xhpc_message']")))
driver.find_element_by_xpath("//div[starts-with(@id, 'u_0_')]//textarea[@name='xhpc_message']").send_keys("Hie")
print("Typed Hie within Facebook Status Box")


使用CSS_SELECTOR:

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

driver = webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("https://www.facebook.com/")
driver.find_element_by_css_selector("input#email").send_keys("email@domain.com")
driver.find_element_by_css_selector("input#pass").send_keys("password")
driver.find_element_by_css_selector("input[id^='u_0_'][value='Log In']").click()
print(driver.title)
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[id^='u_0_'] textarea[name=xhpc_message]")))
driver.find_element_by_css_selector("div[id^='u_0_'] textarea[name=xhpc_message]").send_keys("Hie")
print("Typed Hie within Facebook Status Box")

这篇关于使用硒登录Facebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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