我正在使用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")
这是有效的。从经验出发:在使用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