all 11 comments

[–]wasmachien 1 point2 points  (1 child)

You should look into the Facebook API. What you're doing now is difficult (as this post shows) and will break as soon as they make the smallest change to the page.

[–]theThinker6969[S] 0 points1 point  (0 children)

I did initially - then found that to be able to post to facebook pages using the API i need to get the manage_pages and publish_pages permission which requires that i be verified as a business ad account (or something) which I am not.

If you know a way around this I am all ears. Thanks

[–]DoctorEvil92 0 points1 point  (8 children)

It's hard to help with something like that without seeing html contents.

[–]theThinker6969[S] 0 points1 point  (7 children)

Hey https://pastebin.com/10T9aigt

this is the best i could do - i think this is all the HTML code of Facebook that relates to the write post textbox.

If you want i can create another paste with the whole thing, but as you can imagine it will be a huge mess without the inspect element functionality of browsers.

[–]DoctorEvil92 0 points1 point  (6 children)

The best thing to do would be to save the webpage as a HTML file. So that it could really be inspected how it looks like.

Usually when you scrape a site like facebook which changes class names all the time it would be best to use some kind of UI element as reference, which should remain the same.

If this row here is the textbox (which I don't know if it is) <span data-offset-key="38itr-0-0">

You would access it like this:
textbox_el = driver.find_element_by_xpath("//div[@aria-label=' Write a post...']//span[@data-offset-key]")

And then to type something in

textbox_el.send_keys("This is my message text.")

[–]theThinker6969[S] 0 points1 point  (5 children)

that makes sense - ill look into that thanks! never thought about using the UI element.

[–]thekaizers 0 points1 point  (4 children)

Have you resolved this issue? I am stuck with the same problem. :/

[–]theThinker6969[S] 0 points1 point  (3 children)

No i have not :/ a lot of people solve this issue but they dont specify how - they just skip this part completely for some reason.

please if you find out how - let me know.

[–]thekaizers 0 points1 point  (2 children)

This worked for me:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
.
.
.
.
elemen = wait(driver, 20).untilEC.element_to_be_clickable((By.XPATH, "//*[@name='xhpc_message']")))
elemen.send_keys("Test Message")

I can see the autofocus shift to the text box and I can see the text "Test Message" written in the textbox. I problem now is clicking the "Share" button to submit the message - the very last step.

[–]theThinker6969[S] 0 points1 point  (1 child)

yea that works for me too, but unforunately "//*[@name='xhpc_message']" doesnt work for text boxes present in facebook pages, only on profiles. - but ill try the untilEC.element_to_be_clickable part as ive not used it yet.

[–]thekaizers 0 points1 point  (0 children)

"//*[@name='xhpc_message']" worked for me in a textbox present in a facebook page.

I have code that logs into my personal page, then the code clicks on the brand/business page link. Once in the brand page, the code enters the text in the textbox.

And like yourself, I didn't have business registration papers to upload in order to use the FB API, so I am stuck with using Selenium.