biplab
biplab

Reputation: 55

locating textbox in facebook messenger

i am a noob python coder. i started learning selenium module and now i want to send a text using it but i am struggling to find textbox as the xpath of textbox is changing every time browser is refreshed

from selenium import webdriver
import time

i=1
text=input("enter the text messege")
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)
#driver=webdriver.Chrome(r"chromedriver")
driver.get(r"https://www.facebook.com/messages/t/2585929161530825/")
time.sleep(7)
email_box=driver.find_element_by_xpath(r'//*[@id="email"]')
email_box.click()
email_box.send_keys("[email protected]")
time.sleep(3)
password_box=driver.find_element_by_xpath(r'//*[@id="pass"]')
password_box.click()
password_box.send_keys("rockyou.txt")
time.sleep(3)
login_button=driver.find_element_by_xpath(r'//*[@id="loginbutton"]')
login_button.click()
time.sleep(10)
'''remove_exception=driver.find_element_by_xpath(r'//*[@id="mount_0_0_DP"]/div/div[1]/div/div[3]/div/div/div[1]/div[1]/div[2]/div/div/div/div/div/div[1]/div[2]/div/div/div/div[1]/div[1]/div/div/div[3]/div/div[15]/div[2]/div[3]/div/div/div/div[1]/div/div[1]/span/div/div')
remove_exception.click()
time.sleep(4)'''
#pop_up=driver.find_element_by_xpath(r'')
while i!=3000:
     chat_box=driver.find_element_by_class_name(r'ecm0bbzt e5nlhep0 buofh1pr jq4qci2q a3bd9o3v iko8p5ub eg9m0zos ni8dbmo4 rq0escxv lexh0w6q')
     chat_box.click()
     chat_box.send_keys(text,"{}".format(i))
     send_button=driver.find_element_by_xpath(r'//*[@id="mount_0_0_Td"]/div/div[1]/div/div[3]/div/div/div[1]/div[1]/div[2]/div/div/div/div/div/div[1]/div[2]/div/div/div/div[2]/div/form/div/div[3]/span[2]/div/svg')
     send_button.click()
     if(i==5):
          time.sleep(2)
     i+=1

Upvotes: 0

Views: 425

Answers (1)

Prophet
Prophet

Reputation: 33361

You are using a wrong locator.
Well, Facebook elements locators are not nice :)
Please try this:

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

wait = WebDriverWait(driver, 10)

chat_box_css = 'div[aria-describedby]'
send_btn_css = 'div[aria-label="Press Enter to send"]'
while i!=3000:
     chat_box=driver.find_element_by_css_selector(chat_box_css)
     chat_box.click()
     chat_box.send_keys(text,"{}".format(i))
     wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, send_btn_css))).click()
     

Upvotes: 1

Related Questions