I'm trying to make a bot in Python that directly messages a user of my choice. I was able to code everything up and until selecting 'Message' on the persons profile.
Part of the code that works:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep, strftime
from random import randint
chromedriver_path = 'C:/Users/JACOB/Downloads/chromedriver_win32 (1)/chromedriver.exe'
webdriver = webdriver.Chrome(executable_path=chromedriver_path)
sleep(2)
webdriver.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
sleep(3)
username = webdriver.find_element_by_name('username')
username.send_keys('MYUSERNAME')
password = webdriver.find_element_by_name('password')
password.send_keys('MYPASSWORD')
button_login = webdriver.find_element_by_css_selector('#react-root > section > main > div > article > div > div > div > form > div > button.sqdOP.L3NKy.y3zKF')
button_login.click()
sleep(4)
notnow = webdriver.find_element_by_css_selector('#react-root > section > main > div > div > div > div > button.sqdOP.yWX7d.y3zKF')
notnow.click()
sleep (4)
postnotifications = webdriver.find_element_by_xpath('/html/body/div[4]/div/div/div/div[3]/button[2]')
postnotifications.click()
sleep(3)
mydms = webdriver.find_element_by_xpath('//*[@id="react-root"]/section/nav/div[2]/div/div/div[3]/div/div[2]/a/svg')
mydms.click()
search = webdriver.find_element_by_xpath('//*[@id="react-root"]/section/nav/div[2]/div/div/div[2]/input')
search.send_keys('INSTAGRAM_USERNAME')
sleep(2)
foundit = webdriver.find_element_by_xpath('//*[@id="react-root"]/section/nav/div[2]/div/div/div[2]/div[2]/div[2]/div/a/div/div[2]/div/span')
foundit.click()
sleep(3)
This code takes me all the way to a users page. However when I attempt to 'click' on the Message button, the xpath can't be found. Here is the code:
message = webdriver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/header/section/div[2]/div[1]/div/button')
message.click()
I found the xpath by right clicking on the Message button > Inspect > Highlighting the HTML code on the right hand side > Copy > Copy XPath.
I've also tried the full xpath and it can't seem to find it either.
Here is a screenshot of the button I'm referring too:
I copied your code, and tried to make what you want. I manage to make selenium go to the messages of the user you want to send the message to. Here is the code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep, strftime
from random import randint
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
chromedriver_path = 'C:/Users/JACOB/Downloads/chromedriver_win32 (1)/chromedriver.exe'
webdriver = webdriver.Chrome()
webdriver.maximize_window()
webdriver.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
WebDriverWait(webdriver, 20).until(EC.presence_of_element_located((By.NAME, 'username')))
username = webdriver.find_element_by_name('username')
username.send_keys('username')
password = webdriver.find_element_by_name('password')
password.send_keys('password')
button_login = webdriver.find_element_by_css_selector('#react-root > section > main > div > article > div > div > div > form > div > button.sqdOP.L3NKy.y3zKF')
button_login.click()
WebDriverWait(webdriver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, '#react-root > section > main > div > div > div > div > button.sqdOP.yWX7d.y3zKF')))
notnow = webdriver.find_element_by_css_selector('#react-root > section > main > div > div > div > div > button.sqdOP.yWX7d.y3zKF')
notnow.click()
WebDriverWait(webdriver, 20).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[4]/div/div/div/div[3]/button[2]')))
postnotifications = webdriver.find_element_by_xpath('/html/body/div[4]/div/div/div/div[3]/button[2]')
postnotifications.click()
WebDriverWait(webdriver, 20).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/section/nav/div[2]/div/div/div[3]/div/div[3]/a')))
WebDriverWait(webdriver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@id="react-root"]/section/nav/div[2]/div/div/div[2]/input')))
search = webdriver.find_element_by_xpath('//*[@id="react-root"]/section/nav/div[2]/div/div/div[2]/input')
sleep(2)
search.send_keys('Username')
WebDriverWait(webdriver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@id="react-root"]/section/nav/div[2]/div/div/div[2]/div[2]/div[2]/div/a/div/div[2]/div/span')))
foundit = webdriver.find_element_by_xpath('//*[@id="react-root"]/section/nav/div[2]/div/div/div[2]/div[2]/div[2]/div/a/div/div[2]/div/span')
foundit.click()
WebDriverWait(webdriver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@id="react-root"]/section/main/div/header/section/div[1]/div[1]/div/button')))
webdriver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/header/section/div[1]/div[1]/div/button').click()
I replace all the sleep() functions with the line to wait that the element is loaded. I think that your problem was that your script tried to locate the element before it was loaded.
Before clicking the « message » button, you should put this line:
WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.XPATH, ‘//*[@id="react-root"]/section/main/div/header/section/div[2]/div[1]/div/button’)))
And import this:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
It will wait until the button is loaded and then click it.
If it is still not working, you can just try other methods to click the button, like css_selector, class_name Id...
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.