0

I am trying to make a script, which would share a particular post along some groups in Facebook.
Everything is working smoothly, until choosing the group to repost the post in.

I have decided to use search bar and pressing TAB + ENTER after the search to proceed with the first available option after the search

Testing this manually works, I search for the group, then press tab + enter and the first group is chosen, only publishment is required after.

But trying to do so with Selenium doesn't work.
I can see how the scipt presses TAB, but shortly after, the search bar is being active again, so pressing ENTER doesn't do anything.

try:
            group_input = driver.find_element(
                By.XPATH, '//input[contains(@placeholder, "груп") or contains(@placeholder, "group")]'
            )
            group_input.clear()
            group_input.send_keys(group_name)

            WebDriverWait(driver, 10).until(EC.presence_of_element_located(
                (By.XPATH, '//input[contains(@placeholder, "груп") or contains(@placeholder, "group")]')
            ))
            time.sleep(2)

            group_input.send_keys(Keys.TAB)
            time.sleep(1)
            group_input.send_keys(Keys.ENTER)
            time.sleep(2)

I cannot really understand what is the issue here, how can I do so the group after searching is selected?

I tried switching by simulating the mouse click, tried to do so with execution with js script, but seems like selenium is struggling with not static elements.
I am expecting the script not making the search bar active again after pressing tab.

2 Answers 2

3

I have found what the issue is.
It was not a "Tab" that was not pressing, but "Enter" which was being pressed in "group.input", so it was not targeted on the page, but on the search bar itself.

I have fixed it this way and now it works for me:

actions = ActionChains(driver)
try:
            group_input = driver.find_element(
                By.XPATH, '//input[contains(@placeholder, "груп") or contains(@placeholder, "group")]'
            )
            group_input.clear()
            group_input.send_keys(group_name)

            WebDriverWait(driver, 10).until(EC.presence_of_element_located(
                (By.XPATH, '//input[contains(@placeholder, "груп") or contains(@placeholder, "group")]')
            ))
            time.sleep(2)

            group_input.send_keys(Keys.TAB)
            time.sleep(1)
            actions.send_keys(Keys.ENTER)
            actions.perform()
            time.sleep(2)
Sign up to request clarification or add additional context in comments.

Comments

1

Here's a more robust version of what you're trying to achieve without using the unreliable fixed delay(time.sleep()):

# Locate and wait for the search input for groups 
group_input = wait.until(EC.presence_of_element_located(
    (By.XPATH, '//input[contains(@placeholder, "груп") or contains(@placeholder, "group")]')
))
# Clear any pre-filled text and submit the query
group_input.clear()
group_input.send_keys("Group Name") # for example "Computer Vision"
group_input.send_keys(Keys.ENTER)

# Wait until the first search result container is visible.
wait.until(EC.visibility_of_element_located(
    (By.CSS_SELECTOR, 'div[aria-label="Search results"]>div>div>div>div>div>div:nth-child(1)')
))
# the first group in the search results
first_group = wait.until(EC.visibility_of_element_located(
        (By.CSS_SELECTOR, 'a[aria-label^="Profile photo of"]')
    ))
# clcik to open the group 
first_group.click()
# next, you can proceed to post... 

full code: GitHub

1 Comment

Thank you, that is very useful actually, the code would work even with slow internet speed However, I think I would still need to add some time.sleep with random amount of seconds after each step so the account will not be banned

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.