0

enter image description here

   <div data-offset-key="hch7-0-0" class="_1mf _1mj"><span data-offset- 
    key="hch7-0-0"><br data- 
   text="true"></span></div>
  <span data-offset-key="hch7-0-0"><br data-text="true">
  <span data-text="true"></span>  

I want to enter data in this last span tag with data-text=true, how do I do that with selenium(python)?

My code:(want to post something on the wall of a group on facebook), I opened the post box successfully but I cant enter any input in that. That input is supposed to go in the last span tag in the HTML code above.

 path = 'C:\Program Files (x86)\chromedriver.exe'

 def fun(gmailID,password):
     driver = webdriver.Chrome(path)
     driver.get('https://www.facebook.com/')
     user = driver.find_element_by_id('email')
     user.send_keys(gmailID)
     pass_enter = driver.find_element_by_id("pass")
     time.sleep(1)
     pass_enter.send_keys(password)
     pass_enter.send_keys(Keys.RETURN)
     time.sleep(5)
     fb_link = 'https://www.facebook.com/groups/'
     group_id = 'writersnbloggers'

     driver.get(fb_link+group_id)
      time.sleep(3)
      message = driver.find_element_by_xpath("//*[contains(text(), 
     'Shivam?')]")
     message.click()
     time.sleep(2)
     post_box = driver.find_element_by_xpath('//*span[@data-text="true"]')
     print(post_box)
     post_box.send_keys("hello")
     time.sleep(10)    

if __name__=="__main__":
      fun(id,pwd)
4
  • 1
    where is your code?? Commented Mar 4, 2021 at 11:17
  • please ad screen shot also to know wat kind of element is it Commented Mar 4, 2021 at 11:56
  • code added @Wonka Commented Mar 4, 2021 at 13:48
  • screenshot attached @PDHide Commented Mar 4, 2021 at 13:48

2 Answers 2

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


driver = webdriver.Chrome()

driver.get("https://www.facebook.com")
driver.find_element_by_xpath('//*[contains(text(),"What\'s on your mind")]').click()
WebDriverWait(driver, 10).until(EC.presence_of_element_located(
    (By.CSS_SELECTOR, 'div[role="dialog"] form[method="POST"]')))

driver.switch_to.active_element.send_keys("something")

you can use switch to active element instead ,to focus on the current active element that is the create post

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this works, can you explain how did you find where to write('div[role="dialog"] form[method="POST"]') after you clicked the whats on your mind box
That's the dialogue that opens up after you click that field
Thanks, i got it now @PDHide
1

Try this python code:

WebDriver driver;
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.querySelector(\"div._1mj>span:last-child\").setAttribute(\"data-text\", \true\");");

Demonstration how it will work:

var lastSpan = document.querySelector("div._1mj>span:last-child");
console.log("Before : ");
console.log(lastSpan);
lastSpan.setAttribute("data-text", "true");
console.log("After : ");
console.log(lastSpan);
<div class="_1mf _1mj" data-offset-key="hch7-0-0">
  <span data-offset-key="hch7-0-0"><br data-text="true"></span>
  <span data-offset-key="hch7-0-0"><br data-text="true"></span>
  <span ></span>
</div>

2 Comments

I'm not that comfortable with js, will love if someone can solve this with python.
the above cell of code is python code. Below are HTML and JS just to demonstrate how it will work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.