Logging Facebook using selenium

后端 未结 3 583
盖世英雄少女心
盖世英雄少女心 2021-01-23 03:06

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 selen         


        
相关标签:
3条回答
  • 2021-01-23 03:40

    This Code is to log in to Facebook and Post "Hello World" text in status

    public void facebookLogin() throws InterruptedException {
    
     //1. Set gecko driver path
    
    System.setProperty("webdriver.gecko.driver","C:\\Selenium\\selenium-java-3.0.1\\geckodriver.exe");
    
    WebDriver d= new FirefoxDriver();
    
      //2. Enter URL 
    
      d.get("https://www.facebook.com/");
    
      //3. maximize window
    
      d.manage().window().maximize();
      Thread.sleep(2000);
    
       //4. Login Into Fcaebook
      d.findElement(By.id("email")).sendKeys("email");
      d.findElement(By.id("pass")).sendKeys("password");
      d.findElement(By.id("loginbutton")).click();
       Thread.sleep(3000);
    
        //5. Post "Hello World" into status 
    
       d.get("https://www.facebook.com/");
       WebElement post= d.findElement(By.xpath("//*[@name='xhpc_message']"));
       post.click();
       post.sendKeys("Hello World");
       d.findElement(By.xpath("(//button[@value='1'])[5]")).click();
    
       Thread.sleep(2000);
    
      }
    
    0 讨论(0)
  • 2021-01-23 03:43

    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:


    Using XPATH :

    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")
    

    Using 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")
    
    0 讨论(0)
  • 2021-01-23 03:44

    Have you tried just finding the element by ID as that Xpath looks very likely to change?

    driver.FindElement(By.Id("js_kk")).SendKeys(status);

    0 讨论(0)
提交回复
热议问题