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()
Related
I am trying to scraping in this URL, dealing with a Download button and I am having a problem, as the last line gives a ElementClickInterceptedException.
My actual goal is to download the CSV file.
The code:
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
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.chrome.options import Options
from ipykernel import kernelapp as app
import time
options = webdriver.ChromeOptions()
driver_path = 'C:\\Users\\Idener\\Downloads\\chromedriver_win32\\chromedriver.exe'
driver = webdriver.Chrome(driver_path, options=options)
url = "https://pubchem.ncbi.nlm.nih.gov/compound/2078"
driver.get(url)
wait = WebDriverWait(driver, 5)
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="NIOSH-Toxicity-Data"]/div[1]/div/div/a'))).click()
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="Download"]'))).click()
enter image description here
Element you trying to click in initially out of the visible viewpoint. So, you need first to scroll the page and only then to click on that element.
By clicking the first element new tab is opened and the second element you want to click is there, on the second tab. So, you need to switch to the new tab to access that element.
No need to define wait = WebDriverWait(driver, 10) second time.
The following code is working:
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)
url = "https://pubchem.ncbi.nlm.nih.gov/compound/2078#section=Toxicity"
driver.get(url)
element = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#NIOSH-Toxicity-Data a[title*='Open']")))
element.location_once_scrolled_into_view
time.sleep(1)
element.click()
driver.switch_to.window(driver.window_handles[1])
wait.until(EC.element_to_be_clickable((By.ID, "Download"))).click()
It does not download the file, only opens the downloading dialog
enter image description here
I want to make a web scraper for the news title on the news website, news.sky.com
The problem is, very often, the site will pop up a message to let me "accept all cookies" (see capture...)
I followed the guide on the similar question by using XPath. But, it feedbacks:
raise TimeoutException(message, screen, stacktrace)
Seemly, selenium can't find out the location based on XPath.
so, is it possible to click "Accept all"?
please help, thx.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.common.by import By
e_driver_path = r"F:/Download/Portable Test/msedgedriver.exe"
# establish the web driver
s = Service(e_driver_path)
driver = webdriver.Edge(service=s)
driver.get("https://news.sky.com/uk")
# search = driver.find_element_by_id(ContentPlaceHolder1_NotifyBtn)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[#id='notice']/div[3]/button[1]"))).click()
print("yes")
The cookie buttons are in an iframe on that page. Here is one way of clicking that button:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time as t
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument("window-size=1280,720")
webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
driver = webdriver.Chrome(service=webdriver_service, options=chrome_options)
actions = ActionChains(driver)
wait = WebDriverWait(driver, 20)
url = "https://news.sky.com/uk"
driver.get(url)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[#title='SP Consent Message']")))
try:
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[title='Accept all']"))).click()
print('accepted cookies')
except Exception as e:
print('no cookie button!')
driver.switch_to.default_content()
print('back to main content')
This will access the iframe, click the button, and then exit the iframe back to main content, and also print in terminal:
accepted cookies
back to main content
Selenium setup is chrome/chromedriver/linux, you just need to observe the import and the code after defining the driver, to adapt it to your own setup. Selenium docs can be found here: https://www.selenium.dev/documentation/
How would I scroll in the following window on Instagram using Selenium and Python? I've tried everything I've found and none of them work.
Here's what I have to get me to the following window:
from selenium import webdriver
browser = webdriver.Firefox(executable_path = '/usr/local/bin/geckodriver')
browser.implicitly_wait(5)
browser.get('https://www.instagram.com/')
sleep(2)
username_input = browser.find_element_by_css_selector("input[name='username']")
password_input = browser.find_element_by_css_selector("input[name='password']")
username_input.send_keys("Enter Username Here")
password_input.send_keys("Enter Password Here")
login_button = browser.find_element_by_xpath("//button[#type='submit']")
login_button.click()
sleep(5)
browser.find_element_by_xpath('/html/body/div[1]/section/main/div/div/div/section/div/button').click()
sleep(2)
browser.find_element_by_css_selector('button.aOOlW:nth-child(2)').click()
browser.get('https://www.instagram.com/instagram/')
sleep(2)
browser.find_element_by_css_selector('li.Y8-fY:nth-child(3) > a:nth-child(1) > div:nth-child(1)').click()
sleep(2)
follower_number =int( driver.find_elements_by_xpath('//span [#class="g47SY "]')[2].text)
i=0
while(i<follower_number):
element = driver.find_elements_by_xpath("//div[#role='dialog']//ul//li")
driver.execute_script("arguments[0].scrollIntoView(true);",element[i])```
have a look at this Question. i tried that and worked.
my code
from selenium.webdriver.common.action_chains import ActionChains
import time
import re
from webdriver_manager.chrome import ChromeDriverManager
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
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get("https://www.instagram.com/")
time.sleep(5)
my_email=driver.find_element_by_xpath('//*[#id="loginForm"]/div/div[1]/div/label/input')
my_email.send_keys("")
my_password=driver.find_element_by_xpath('//*[#id="loginForm"]/div/div[2]/div/label/input')
my_password.send_keys("")
time.sleep(5)
login=driver.find_element_by_xpath('//*[#id="loginForm"]/div/div[3]')
login.click()
time.sleep(5)
driver.get("https://www.instagram.com/instagram/following/")
driver.find_element_by_css_selector('li.Y8-fY:nth-child(3) > a:nth-child(1) > div:nth-child(1)').click()
time.sleep(5)
# this is not perfect but it will get your work done
# getting the number of followers
follower_number =int( driver.find_elements_by_xpath('//span [#class="g47SY "]')[2].text)
i = 0
# looping for the exact number of followers ...
while(i<follower_number):
# as the website is dyanamic so updating the follwers list and also the webelement
element = driver.find_elements_by_xpath("//div[#role='dialog']//ul//li")
# executing scroll into view script to view the element and thats gonna load the next element(follower ) ..ultimately your scrolling achived
driver.execute_script("arguments[0].scrollIntoView(true);",element[i])
time.sleep(2)
print(i)
i=i+1
I have been able to get past the pop up in my code but I am not able to then type in the email and password for the account via sending the keys and then hit the login button.
Here is the code I am using and the site I am trying to login to. I am VERY new so any response please try to explain so I am able to learn from my mistakes.
Thanks so much in advance!
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
from selenium.webdriver.common.action_chains import ActionChains
import time
from time import sleep
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
PATH = "C:\Program Files\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.implicitly_wait(5)
driver.get("https://www.finewineandgoodspirits.com/webapp/wcs/stores/servlet/LogonForm?langId=-1&storeId=10051&catalogId=10051")
main_page = driver.current_window_handle
sleep(1)
driver.find_element_by_xpath('//*[contains(text(), "at least 21 years old.")]').click()
sleep(1)
driver.find_element_by_xpath('//*[contains(text(), "at least 21 years old.")]/span').click()
for handle in driver.window_handles:
if handle != main_page:
login_page = handle
driver.switch_to.window(login_page)
driver.find_element_by_xpath('//*[contains(text(), "at least 21 years old.")]').click()
driver.switch_to.window(main_page)
WebDriverWait(driver,1)
fwgsUsername = credentials.login['candmidlik#yahoo.com']
fwgsPassword = credentials.login['Password2']
emailFieldID = "logonId"
passFieldID = "logonPassword"
loginButtonXpath = "//input[#id = 'loginButton']"
emailFieldElement = WebDriverWait(driver,1).until(lambda driver: driver.find_element_by_name(emailFieldID))
emailFieldElement.clear()
emailFieldElement.send_keys(fwgsUsername)
passFieldElement = WebDriverWait(driver,1).until(lambda driver: driver.find_element_by_name(passFieldID))
passFieldElement.clear()
passFieldElement.send_keys(fwgsPassword)
logInButtonElement = WebDriverWait(driver,1).until(lambda driver: driver.find_element_by_xpath(loginButtonXpath))
logInButtonElement.click()
You should swap over to webdriver wait for the yes since it's not switching windows. Also your xpath for the login was an a tag.
wait=WebDriverWait(driver, 60)
driver.get("https://www.finewineandgoodspirits.com/webapp/wcs/stores/servlet/LogonForm?langId=-1&storeId=10051&catalogId=10051")
wait.until(EC.element_to_be_clickable((By.XPATH,'//span[.="YES"]'))).click()
fwgsUsername = 'candmidlik#yahoo.com'
fwgsPassword = 'Icecream90'
emailFieldID = "logonId"
passFieldID = "logonPassword"
loginButtonXpath = "//a[#id = 'loginButton']"
emailFieldElement = WebDriverWait(driver,1).until(lambda driver: driver.find_element_by_name(emailFieldID))
emailFieldElement.clear()
emailFieldElement.send_keys(fwgsUsername)
passFieldElement = WebDriverWait(driver,1).until(lambda driver: driver.find_element_by_name(passFieldID))
passFieldElement.clear()
passFieldElement.send_keys(fwgsPassword)
logInButtonElement = WebDriverWait(driver,1).until(lambda driver: driver.find_element_by_xpath(loginButtonXpath))
logInButtonElement.click()
Imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
To send a character sequence within the E-mail field you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://www.finewineandgoodspirits.com/webapp/wcs/stores/servlet/LogonForm?langId=-1&storeId=10051&catalogId=10051")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='YES']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.emailInput_myacc[name='logonId']"))).send_keys("Candcrush#Candcrush.Candcrush")
driver.find_element(By.CSS_SELECTOR, "input.emailInput_myacc[name='logonPassword']").send_keys("Candcrush")
driver.find_element(By.CSS_SELECTOR, "a#loginButton").click()
Using XPATH:
driver.get("https://www.finewineandgoodspirits.com/webapp/wcs/stores/servlet/LogonForm?langId=-1&storeId=10051&catalogId=10051")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='YES']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='emailInput_myacc' and #name='logonId']"))).send_keys("Candcrush#Candcrush.Candcrush")
driver.find_element(By.XPATH, "//input[#class='emailInput_myacc' and #name='logonPassword']").send_keys("Candcrush")
driver.find_element(By.XPATH, "//a[#id='loginButton']").click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
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()