简体   繁体   中英

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

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" class="_8-yf5 " 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?

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM