1

I have no idea why this code does not work properly. This program is for automatically locating respective fields of username, password, and enter some data for the tweet to be sent. I am selecting the elements of the page correctly, but not able to enter data into it and get a

NoSuchElement

Exception .

from selenium import webdriver
import time
import os
import selenium

chromedriver = "mypathto/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)

driver.get('https://twitter.com/login')
username = driver.find_element_by_name("session[username_or_email]")
password= driver.find_element_by_name("session[password]")
username.send_keys("myusername")
password.send_keys("password")



submit = driver.find_element_by_class_name("submit EdgeButton EdgeButton--primary EdgeButtom--medium")
submit.click()

autotw1= driver.find_element_by_id('tweet-box-home-timeline')
autotw1.send_keys("""Just a testing """)
print "Tweet probably sent"

driver.save_screenshot("mybotfunc.png")
5
  • What is the version of chrome, Selenium driver you're using? Commented May 17, 2018 at 16:20
  • @SarveshEB why this code does not work properly where are you exactly stuck? Commented May 17, 2018 at 16:37
  • After finding the username and password element by field, I am not able to do ' driver.send_keys("username") ' . It throws an 'ElementNotVisible' error Commented May 17, 2018 at 16:43
  • 1
    @Sarvesh EB : check my answer and update us with result ! Commented May 17, 2018 at 16:54
  • 1
    @JeffC The problem statement has been explicitly stated in the title Commented May 17, 2018 at 17:18

1 Answer 1

2

You can use this code :

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

driver = webdriver.Chrome(executable_path = r'D:/Automation/chromedriver.exe')
driver.get("https://twitter.com/login")

username = driver.find_element_by_css_selector("input[placeholder='Phone, email or username']")
password= driver.find_element_by_css_selector("input[class='js-password-field']")
username.send_keys("your username")
password.send_keys("your password")

submit = driver.find_element_by_xpath("//button[text()='Log in']")
submit.click()

autotw1 = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "div[id='tweet-box-home-timeline']")))
autotw1.send_keys("""Just a testing """)  

tweet = driver.find_element_by_xpath("//span[@class='add-tweet-button ']//following-sibling::button[contains(@class,'tweet-action')]")
tweet.click()

Why your code is not working :

there are 3 element available with this name : session[username_or_email]
Several with this : session[password]
and 2 with this id : tweet-box-home-timeline

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

7 Comments

Okay, your code works, but when I add another line of code tweet= driver.find_element_by_xpath("//button[text()='Tweet']") tweet.click() It fails to do that, since that is the sole purpose of this program. Also, can you debug or explain why my code doesn't work
answer my above comment please. I am no able to send the tweet.
there is something wrong with xpath : //button[text()='Tweet'] , I am trying to solve it
@JeffC Sir, I never urged anyone. If my tagging has bothered someone, I will avoid it.
@Sarvesh EB use this : tweet = driver.find_element_by_xpath("//span[@class='add-tweet-button ']//following-sibling::button[contains(@class,'weet-action')]") tweet.click()
|

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.