can`t send tweet with selenium and python - python

hi there i need some help with the following code ....
i`m using selenium with python to send tweets automatically :
import time
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'C:/webDriver/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("xxxx")
password.send_keys("xxxx")
submit = driver.find_element_by_xpath("//button[text()='Log in']")
submit.click()
time.sleep(2.4)
autotw1 = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, '//*[#id="tweet-box-home-timeline"]/div'))).click
autotw1.send_keys("""testing """)
time.sleep(5)
tweet = driver.find_element_by_xpath('//span[#class="add-tweet-button "]//following-sibling::button[contains(#class,"tweet-action")]')
tweet.click()
But It gave me error :
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
i think the error came from xpath cause of the twitter website new style !!

You can use this locator:By.CLASS_NAME, 'DraftEditor-root'.
You must click on the element to bring up other elements to write the tweet, which is:By.CLASS_NAME, 'public-DraftEditorPlaceholder-root'), and use ActionChains to send text.
Following import:
from selenium.webdriver import ActionChains
Try the bellow code:
#submit login
submit = driver.find_element_by_xpath("//button[text()='Log in']")
submit.click()
autotw1 = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, 'DraftEditor-root')))
autotw1.click()
element = WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.CLASS_NAME, 'public-DraftEditorPlaceholder-root')))
ActionChains(driver).move_to_element(element).send_keys('testing').perform()
sendTw = WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.LINK_TEXT, 'Tweet')))
sendTw.click()

Related

Python + Selenium TimeoutException

This is my first time using Python and Selenium. The first part of the code works but when it goes to the second page it can never find any of the elements. If I flip the code and make it go to the second site first, it works. What am I doing wrong here? I tried xpath, CSS_Selector, Class_Name seems like nothing is working. This is the error I get:
Traceback (most recent call last):
File "C:\Users\dresd\PycharmProjects\Test2\main.py", line 20, in
click_Register = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CLASS_NAME, "q-text qu-ellipsis qu-whiteSpace--nowrap"))).click()
File "C:\Users\dresd\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\support\wait.py", line 89, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Chrome ("C:/chromedriver.exe")
driver.get("https://10minutesemail.net/")
Copy_Email = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.ID, "copyEmailAddress"))).click()
time.sleep(10)
driver.execute_script("window.open('https://quora.com/','_blank')")
click_Register = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.CLASS_NAME, "q-text qu-ellipsis qu-whiteSpace--nowrap"))).click()
name = driver.find_element(By.NAME, "profile-name")
email = driver.find_element(By.ID, "email")
name.send_keys("Jackson Fuller")
Thanks in advance!
You have to switch the driver to the new opened tab.
Without that the focus will remain on the first browser window.
TimeoutException actually means that Selenium could not locate element by passed locator.
Also the locator you are using is bad.
Try this:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Chrome ("C:/chromedriver.exe")
driver.get("https://10minutesemail.net/")
Copy_Email = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.ID, "copyEmailAddress"))).click()
time.sleep(10)
driver.execute_script("window.open('https://quora.com/','_blank')")
driver.switch_to.window(driver.window_handles[1])
click_Register = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.CLASS_NAME, "q-text qu-ellipsis qu-whiteSpace--nowrap"))).click()
name = driver.find_element(By.NAME, "profile-name")
email = driver.find_element(By.ID, "email")
name.send_keys("Jackson Fuller")

Posting a tweet in twitter using python selenium for a poc

I am trying to post a tweet in Twitter using python and selenium
But at the time of posting it is giving me timeout error.
I have the code ready....
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
import time
import os
# create a new Chrome session
chromedriver = "C:/Users/LENOVO/Desktop/chromedriver/chromedriver.exe"
chromedriver = "C:/Users/LENOVO/Desktop/chromedriver/chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
#driver = webdriver.Chrome()
driver.implicitly_wait(30)
driver.maximize_window()
# navigate to the application home page
driver.get("https://twitter.com/login")
# get the username textbox
login_field = driver.find_element_by_class_name("js-username-field")
login_field.clear()
# enter username
login_field.send_keys("")
time.sleep(1)
#get the password textbox
password_field = driver.find_element_by_class_name("js-password-
field")
password_field.clear()
#enter password
time.sleep(10)
password_field.send_keys("")
time.sleep(10)
password_field.submit()
autotw1 = WebDriverWait(driver, 140).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()
I am getting the below error.
TimeoutException Traceback (most recent call last)
in
39
40 autotw1 = WebDriverWait(driver, 140).until(
---> 41 EC.element_to_be_clickable((By.CSS_SELECTOR, "div[id='tweet-box-home-timeline']")))
You can use this locator:By.CLASS_NAME, 'DraftEditor-root'.
You must click on the element to bring up other elements to write the tweet, which is:By.CLASS_NAME, 'public-DraftEditorPlaceholder-root'), and use ActionChains to send text.
First, following import:
from selenium.webdriver import ActionChains
After submit login please try below:
password_field.submit()
autotw1 = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, 'DraftEditor-root')))
autotw1.click()
element = WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.CLASS_NAME, 'public-DraftEditorPlaceholder-root')))
ActionChains(driver).move_to_element(element).send_keys("""Just a testing """).perform()
sendTw = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//div[#role="button"]/div/span/span')))
sendTw.click()
You have provided incorrect xpath and css value in your script. Kindly run below scrip with your own credentials and don't forget to update chrome driver path.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import WebDriverWait as Wait
from selenium.webdriver.common.action_chains import ActionChains
# Open Chrome
driver = webdriver.Chrome('Path to chrome driver')
driver.get("https://twitter.com/login")
# get the username textbox
login_field = driver.find_element_by_class_name("js-username-field")
login_field.clear()
# enter username
login_field.send_keys("")
time.sleep(1)
# get the password textbox
password_field = driver.find_element_by_class_name("js-password-field")
password_field.clear()
# enter password
password_field.send_keys("")
password_field.submit()
autotw1 = WebDriverWait(driver, 15).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='css-1dbjc4n r-xoduu5 r-1sp51qo r-mk0yit r-13qz1uu']")))
ActionChains(driver).move_to_element(autotw1).click(autotw1).send_keys("Just a testing").perform()
tweet = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='css-901oao css-16my406 css-bfa6kz r-1qd0xha r-ad9z0x r-bcqeeo r-qvutc0']//span[#class='css-901oao css-16my406 r-1qd0xha r-ad9z0x r-bcqeeo r-qvutc0'][contains(text(),'Tweet')]")))
tweet.click()

Click automation using Selenium

For automating downloading from a website, I am using this python script for automating click action. But the click does not work.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
browser = webdriver.Firefox()
browser.maximize_window()
browser.implicitly_wait(20)
browser.get('http://download.cnet.com/most-popular/windows/')
linkElem = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "CCleaner")))
linkElem.click() # follows the "CCleaner" link
I first got this error:
selenium.common.exceptions.ElementNotInteractableException: Message: Element could not be scrolled into view
Then I add this line:
linkElem = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "CCleaner")))
Now I get this error:
linkElem = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "CCleaner")))
File "/usr/lib/python2.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
I tried the things that people used to solve this problem but did not work.
Your code seems to work with Chrome, I only added the bottom two lines and it downloaded CCleaner.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
browser = webdriver.Chrome() # Firefox()
browser.maximize_window()
browser.implicitly_wait(20)
browser.get('http://download.cnet.com/most-popular/windows/')
linkElem = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "CCleaner")))
linkElem.click() # follows the "CCleaner" link
download = browser.find_element_by_id('download-now-btn-text')
download.click()

How can I open a letter in mail.ru using selenium's webdriver?

I try:
driver = webdriver.Firefox()
driver.get('https://e.mail.ru/messages/inbox/')
...
letterTag = driver.find_element_by_css_selector("div.b-datalist__item__addr")
and it gives me:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"div.b-datalist__item__addr"}
I see 'div.b-datalist__item__addr' in develop tools, but I can't find it in page's source code. I suppose it generates by javascript... I need to click this element in order to open a letter. How can I do this?
Link to documentation.
My solution:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
name = input("Login: ")
password = input("Password: ")
driver = webdriver.Chrome()
driver.get('https://mail.ru/')
driver.find_element_by_id('mailbox__login')
mail_name = driver.find_element_by_id('mailbox__login')
mail_name.send_keys(name)
mail_pass = driver.find_element_by_id('mailbox__password')
mail_pass.send_keys(password)
mail_pass.submit()
# get first letter
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "a.js-href.b-datalist__item__link")))
element.click()

Automate The boring Stuff - chapter 11 - Filling out and submitting forms p. 259

I am trying to run the code to log into gmail.com but the login process seems to have changed. I have modified the code as follow but keep getting the following error: selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"id","selector":"Passwd"}
Dose anyone see or know what I am doing wrong. Thanks
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://gmail.com')
emailElem = browser.find_element_by_id('Email')
emailElem.send_keys('some_email#gmail.com')
nextElem = browser.find_element_by_id('next')
nextElem.click() # click the button
passwordElem = browser.find_element_by_id('Passwd')
passwordElem.send_keys('password')
passwordElem.submit()
You need to wait for the presence of element with id="Passwd":
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(browser, 10)
passwordElem = wait.until(EC.presence_of_element_located((By.ID, "Passwd")))
passwordElem.send_keys('password')
passwordElem.submit()

Categories