【问题标题】:How to send keys to fb status box by selenium in python?如何在 python 中通过 selenium 将密钥发送到 fb 状态框?
【发布时间】:2017-07-30 22:08:07
【问题描述】:

我在 python 中使用 selenium,驱动程序是 Chrome,我想在 facebook 墙上发帖。但我无法单击状态框,也无法在其中输入值。任何机构都可以帮助发送密钥并单击发布按钮吗?

driver.get('https://www.facebook.com/username')
time.sleep(4)
driver.find_element_by_xpath("//span[@class='_5qtp']").click()
driver.find_element_by_xpath("//input[@class='status']").send_keys("first post")

【问题讨论】:

  • 为什么不用API?
  • 我应该使用哪个 API?

标签: python selenium web-scraping selenium-chromedriver


【解决方案1】:

这是有效的。经验之谈:在使用 Facebook 时尽量避免使用find_element_by_xpath

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.chrome.options import Options

### login details ########################
username = "xxxxxxx@xxxxx.xxx"
pwd = "xxxxxxxxx"
##########################################

### what is on my mind ###################
msg = "hey there!"
##########################################

# initializing driver and loading web
chrome_path = r"chromedriver.exe"
options = Options()
options.add_argument("--disable-notifications")
driver = webdriver.Chrome(chrome_path, chrome_options=options)
driver.get("http://www.facebook.com/")
# end initializing

# start login
user_input = driver.find_element_by_xpath("""//*[@id="email"]""")
pwd_input = driver.find_element_by_xpath("""//*[@id="pass"]""")
user_input.send_keys(username)
pwd_input.send_keys(pwd)
pwd_input.send_keys(Keys.ENTER)
# end login

# writing msg
time.sleep(3)
first_what_is_on_my_mind_element = driver.find_element_by_class_name("_5qtp")
first_what_is_on_my_mind_element.click()
time.sleep(3)
second_what_is_on_my_mind_element = driver.switch_to.active_element
second_what_is_on_my_mind_element.send_keys(msg)
# end writing

# posting
button = driver.find_element_by_css_selector("._1mf7._4jy0._4jy3._4jy1._51sy.selected._42ft")
button.click()
# end posting

【讨论】:

  • first_what_is_on_my_mind_element = driver.find_element_by_class_name("_5qtp") first_what_is_on_my_mind_element.click() time.sleep(3) second_what_is_on_my_mind_element = driver.switch_to.active_element second_what_is_on_my_mind_element.send_keys(msg) 这不工作
猜你喜欢
  • 2018-03-13
  • 2016-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多