How to use Selenium to Like posts in feed?

Joined
Feb 27, 2023
Messages
7
Reaction score
0
I've been trying to like the posts in my feed by using Selenium but so far have had no luck

Here is my code:

like_buttons = chrome.find_elements_by_xpath("//article//section//button//*[@aria-label='Like']")
for button in like_buttons:
button.click()
time.sleep(1)

I get this error:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

I also tried this:
wait = WebDriverWait(chrome, 30)
like_buttons = wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//article//section//button//*[@aria-label='Like']")))
for button in like_buttons:
button.click()
time.sleep(1)

and it gave me TimeoutException
 
The "ElementNotInteractableException" error occurs when the element you're trying to interact with is not clickable or visible on the page. The "WebDriverWait" method you tried should solve this issue. However, the "TimeoutException" error you received indicates that the element could not be found within the specified timeout period.

You may want to try increasing the timeout duration or using a more specific XPath to locate the like buttons. You could also try using the "execute_script()" method to scroll to the element before attempting to interact with it.

Code:
like_buttons = chrome.find_elements_by_xpath("//button[@aria-label='Like']")
for button in like_buttons:
    try:
        chrome.execute_script("arguments[0].scrollIntoView();", button)
        button.click()
    except:
        pass
    time.sleep(1)

This code uses a more specific XPath to locate the like buttons and scrolls to each button using the "execute_script()" method before attempting to click it. The "try/except" block is used to handle any errors that may occur while clicking the button, such as if the button is not clickable or visible on the page.
 
Back
Top