Reputation: 53
I'm trying to make an Instagram bot to like a post if it's unliked, and to ignore it if it's already liked
I've managed to grab the div element and click it, but for the like detection code I need the attribute for "aria-label"
or "fill"
for the bot to specify if the post is liked or not, but I haven't managed to grab the svg
element to do so.
Here's the like button structure
and here's my code:
like_button = self.driver.find_element_by_xpath(
'//*[@id="react-root"]/section/main/div/div/article/div[3]/section[1]/span[1]/button/div')
like_icon=like_button.find_element_by_xpath(
'//*[@id="react-root"]/section/main/div/div/article/div[3]/section[1]/span[1]/button/div/span/svg')
Whenever i run it an error occurs:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="react-root"]/section/main/div/div/article/div[3]/section[1]/span[1]/button/div/span/svg"}
Upvotes: 1
Views: 2033
Reputation: 193108
The element is a dynamic WebElement, so ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable()
and you can use either of the following Locator Strategies:
Using CSS_SELECTOR
:
like_button = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button span > svg[aria-label='Like']"))).click()
Using XPATH
:
like_button = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button//span//*[name()='svg' and @aria-label='Like']"))).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
You can find a couple of relevant discussions in:
Upvotes: 1