简体   繁体   中英

Tweet using Selenium Python chrome webdriver

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")

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM