Home > OS >  How to click on the Instagram Comment button using Selenium in Python
How to click on the Instagram Comment button using Selenium in Python

Time:04-01

I'm basically trying to write a bot which comments on the most recent post of my timeline. I have issues with finding the comment button using selenium. I have tried every way of finding it but still didnt succeed.

HTML:

<svg aria-label="Kommentar"  color="#262626" fill="#262626" height="24" role="img" viewBox="0 0 24 24" width="24"><path d="M20.656 17.008a9.993 9.993 0 10-3.59 3.615L22 22z" fill="none" stroke="currentColor" stroke-linejoin="round" stroke-width="2"></path></svg>

Code trials:

driver.find_element_by_xpath('/html/body/div\[5\]/div\[2\]/div/article/div\[3\]/section\[3\]/div/form/textarea').click()

Anyone can help me here?

CodePudding user response:

The click on the Comment button Kommentar within Instagram you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "svg[aria-label='Kommentar']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[name()='svg' and @aria-label='Kommentar']"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

CodePudding user response:

Just use ENTER method:

from selenium.webdriver.common.keys import Keys

addComment = find_element_by_xpath('/html/body/div\[5\]/div\[2\]/div/article/div\[3\]/section\[3\]/div/form/textarea')

addComment.send_keys(Keys.ENTER)
  • Related