Find Elements ın Python Selenium

Question:

Im trying to find some button elements with XPATH,CSS_SELLECTOR since yesterday but my codes is not working. I dont understand I have one of the tags I chose in the website but cant find the element what is the problem ?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
#from instagramuser import username,password

class Instagram:
    def __init__(self,username,password):
        self.username = username 
        self.password = password
        self.browser = webdriver.Chrome()`


    def signIn(self):
        self.browser.get("https://www.instagram.com/")
        self.browser.maximize_window()
        time.sleep(2)
        self.browser.find_element(By.XPATH,'//*[@id="loginForm"]/div/div[1]/div/label/input').send_keys(self.username)
        time.sleep(1)
        self.browser.find_element(By.XPATH,'//*[@id="loginForm"]/div/div[2]/div/label/input').send_keys(self.password)
        time.sleep(1)
        self.browser.find_element(By.CSS_SELECTOR,'button[class="_a9-- _a9_0"]').click()
        time.sleep(5)
        
    def getFollowers(self):
        self.browser.get(f"https://www.instagram.com/{self.username}/")
        self.browser.find_element(By.XPATH,'//*[@id="mount_0_0_JS"]/div/div/div[1]/div/div/div/div[1]/div[1]/div[2]/section/main/div/header/section/div[1]/div[1]/div[1]/div/a')

ERROR PHOTO`

Asked By: ofowardar

||

Answers:

Your selector is wrong, here is the correct Xpath:

self.browser.find_element(By.XPATH, '//button[@class="_acan _acap _acas _aj1-"]').click()

Replace this with the one appearing in your error.

And a tip, don’t use time.sleep to wait for page to load, you should use WebdriverWait module, that waits for a maximum period of time for the element to appear, but when it does find it, the program runs instantly.

Example:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

element = WebDriverWait(self.browser, 10).until(ec.presence_of_element_located((By.XPATH, '//button[@class="_acan _acap _acas _aj1-"]')))

# You can do whatever actions you like then

element.click()

element.send_keys()
Answered By: RifloSnake

Follow Button

I must take XPATH or CSS_SELLECTOR this button for take followers but i cant run ….

Answered By: ofowardar