Python Selenium上传照片到Facebook

时间:2015-11-16 08:04:44

标签: python facebook selenium

我曾多次尝试将照片上传到Facebook帖子。当我阅读硒文件时,它说的是

  

选择<input type="file">元素并调用send_keys()方法传递文件路径,相对于测试脚本的路径或绝对路径。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

usr = "random@gmail.com"
pwd = "randompassword"

driver = webdriver.Firefox()
# or you can use Chrome(executable_path="/usr/bin/chromedriver")
driver.get("http://www.facebook.com/login")
assert "Facebook" in driver.title
elem = driver.find_element_by_id("email")
elem.send_keys(usr)
elem = driver.find_element_by_id("pass")
elem.send_keys(pwd)
elem.send_keys(Keys.RETURN)
elem = driver.find_element_by_css_selector("#u_0_y")
elem.send_keys("Hello Internet :) ")
driver.find_element_by_css_selector("._11b").click()

3 个答案:

答案 0 :(得分:0)

请尝试使用xpath。

,而不是使用css_selector

答案 1 :(得分:0)

statuselement = driver.find_element_by_xpath(“ // [@ name ='xhpc_message']”)。click()driver.find_element_by_xpath(“ // [@ class ='_ 3jk']”)。click()l = driver .find_elements_by_tag_name('input')ipath =“ C:\ Users \ Utente \ Pictures \ CoutureBeardsley.jpg” for l in if:if g == driver.find_element_by_xpath(“ // input [@ type ='file'] [@ class ='_ n _5f0v']“):g.send_keys(ipath)print('image loading')

答案 2 :(得分:0)

这对我有用。.

def main():
    # Your Facebook account user and password
    usr = "test.fb.post@gmail.com"
    pwd = "test123456789"





     grp = ['https://www.facebook.com/groups/grpid/', 'https://www.facebook.com/groups/grpid/',
           'https://www.facebook.com/groups/grpid/', 'https://www.facebook.com/groups/grpid/',
            'https://www.facebook.com/groups/grpid/', 'https://www.facebook.com/groups/grpid/',
           'https://www.facebook.com/groups/grpid/']





    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_experimental_option("detach", True)
    chrome_options.add_argument("--disable-infobars")
    chrome_options.add_experimental_option("prefs", { \
        "profile.default_content_setting_values.notifications": 2  # 1:allow, 2:block
    })

    driver = webdriver.Chrome(chrome_options=chrome_options)
    driver.implicitly_wait(15)  # seconds

    # Go to facebook.com
    driver.get("http://www.facebook.com")
    sleep(2)
    # Enter user email
    elem = driver.find_element_by_id("email")
    elem.send_keys(usr)
    # Enter user password
    elem = driver.find_element_by_id("pass")
    elem.send_keys(pwd)
    # Login
    elem.send_keys(Keys.RETURN)
    sleep(10)
    for group in grp:

        driver.get(group)

        try:

            try:

                commentr = WebDriverWait(driver,10).until(EC.element_to_be_clickable( (By.XPATH, "//*[@name='xhpc_message_text']") ))
                commentr.click()

            except Exception:
                commentr = WebDriverWait(driver,10).until(EC.element_to_be_clickable( (By.XPATH, "//*[@loggingname='status_tab_selector']") ))
                commentr.click()



            commentr = WebDriverWait(driver,10).until(EC.element_to_be_clickable( (By.XPATH, "//*[@class='_3u15']") ))
            commentr.click()

            sleep(3)
            l=driver.find_elements_by_tag_name('input') 
            sleep(1)

            for g in l: 
                if g==driver.find_element_by_xpath("//input[@type='file'][@class='_n _5f0v']"): 
                    sleep(1)
                    g.send_keys(ipath) 
                    print('image loaded')




            sleep(10)
            driver.find_element_by_xpath("//*[@class='_1mf _1mj']").send_keys(message)

            sleep(1)
            buttons = driver.find_elements_by_tag_name("button")
            sleep(1)
            for button in buttons:
                    if button.text == "Post":
                        sleep(5)
                        button.click()
                        sleep(10)

        except Exception:
            pass
            print ('image not posted in '+group)


    driver.close()



if __name__ == '__main__':
    main()