Python - Logging in TWITTER using Selenium - python

I am using Python 3.10 and I am trying to log in on Twitter using Selenium. I am not able to get a hold of the input button to enter my user. Looks like the selenium documentation has changed. I could not find a good example.
This is the code I try:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys # not sure why this is greyed out
Chrome_driver_path = "C:/Users/osman/OneDrive/Desktop/chromedriver.exe"
driver = webdriver.Chrome(Chrome_driver_path)
driver.get("https://www.twitter.com/login")
First_Name = driver.find_element(By.XPATH, '//*[#id="react-root"]/div/div/div/main/div/div/div/div[2]/div[2]/div/div[5]/label/div/div[2]/div/input')
First_Name.send_keys('osman#yahoo.com')
And this is the error I get:
executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome(Chrome_driver_path)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="react-root"]/div/div/div/main/div/div/div/div[2]/div[2]/div/div[5]/label/div/div[2]/div/input"}
(Session info: chrome=106.0.5249.119)
Stacktrace:
Backtrace:
Ordinal0 [0x00841ED3+2236115]
Process finished with exit code 1

There are several problems here:
As mentioned, executable_path has been deprecated. You should use Service object instead as following.
Very long absolute XPath locators are very breakable. You have to use relative locators.
You also have to use WebDriverWait expected_conditions to wait for elements to become clickable before iterating them.
And finally from selenium.webdriver.common.keys import Keys is greyed out since you never used this import in your code. So, it's OK, but this line could be removed to make your code clean.
The following code works:
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(service=webdriver_service, options=options)
url = 'https://www.twitter.com/login'
driver.get(url)
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[autocomplete="username"]'))).send_keys("osman#yahoo.com")
This is what happens on www.twitter.com/login after the above code run, screenshot:

Related

No Selenium selectors are working for me python

I understand that this question has been asked a lot in one way or another, however, I have tried finding elements on selenium with every type that I have at my disposal and it keeps giving me the error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:
Am I just grossly misusing selenium or is it the website?
I honestly just want to select the element so that I can start working with it for some practice code that I am doing.
Here is my code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
import time
email = 'example#email.com'
options = Options()
options.binary_location = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
service = Service("/Users/NAME/Desktop/job_stuff/chromedriver")
driver = webdriver.Chrome(options = options, service=service)
driver.get('https://www.hgtv.com/sweepstakes/hgtv-urban-oasis/sweepstakes')
is_open = True
time.sleep(5)
# inputField = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,'//*[#id="xReturningUserEmail"]')))
inputField = driver.find_element(By.XPATH, '/html/body/div[1]/div/main/section/div/div/div/div/div/div[1]/div/form/div[1]/fieldset/div/div[2]/div[1]/input')
It is an iframe.
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(driver.find_element(By.XPATH, ".//*[starts-with(#id,'ngxFrame')]")))
driver.find_element(By.XPATH,".//input[#type='email' and #id='xReturningUserEmail']").send_keys("test#gmail.com")

selenium find_element_by_css_selector for the long class name

I have tried multiple times with other instruction codes with space from the tutorial which worked fine. However, when I just changed the URL and the following class, it would give the error saying
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified
(Session info: chrome=100.0.4896.88)
Everything worked when I used the tutorial code.
Here is my code (I have solved a few chrome driver problems from the internet)
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome(options=options)
driver.get("https://raritysniper.com/nft-drops-calendar")
time.sleep(1)
link = driver.find_element_by_css_selector(".w-full.h-full.align-middle.object-cover.dark:brightness-80.dark:contrast-103.svelte-f3nlpp").get_attribute("alt")
print(link)
I am trying to get attributes of each projects and make them into csv.
(Please refer to the screenshot)
Screen shot of HTML that I am trying to extract
it would be wonderful if anyone could depict the problem I got with the code.
Thank you!
The CSS_SELECTOR that you are using
.w-full.h-full.align-middle.object-cover.dark:brightness-80.dark:contrast-103.svelte-f3nlpp
does not really match any element in the HTML.
Instead, you should use this CSS_SELECTOR:
div.w-full.h-full.align-middle img:not(.placeholder)
In code:
driver.maximize_window()
wait = WebDriverWait(driver, 30)
driver.get("https://raritysniper.com/nft-drops-calendar")
#time.sleep(1)
first_element = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.w-full.h-full.align-middle img:not(.placeholder)")))
print(first_element.get_attribute('alt'))
print(first_element.get_attribute('src'))
Import:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Output:
NEON PLEXUS
https://media.raritysniper.com/featured/neon-plexus_1648840196045_3.webp
Process finished with exit code 0

Unable to fill the form using Selenium: AttributeError: 'WebDriver' object has no attribute 'get_element_by_xpath'

I am trying to fill the Uber form
using selenium.
It looks like this:
I tried to use XPath to fill the form, here is the code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("user-data-dir=C:\\Users\\Zedas\\AppData\\Local\\Google\\Chrome\\User Data")
w = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=options)
w.get("https://m.uber.com/looking")
time.sleep(6)
w.get_element_by_xpath(f'"//*[#id="booking-experience-container"]/div/div[3]/div[2]/div/input"').send_keys("test")
But, this is the error:
w.get_element_by_xpath(f'"//*[#id="booking-experience-container"]/div/div[3]/div[2]/div/input"').send_keys("test")
AttributeError: 'WebDriver' object has no attribute 'get_element_by_xpath'
How can this error be fixed?
There's no method known as get_element_by_xpath in Selenium-Python bindings.
Please use
driver.find_element_by_xpath("xpath here")
Also, Since find_element internally looks for implicit waits to wait and interact with web element. Often, It has been observed that it is not a consistent method to look for web element/elements in Selenium automation.
Please use Explicit waits :
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "xpath here"))).send_keys("test")
You will need these imports as well.
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
You can read more about waits from here
w.get_element_by_xpath should be w.find_element_by_xpath
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("user-data-dir=C:\\Users\\Zedas\\AppData\\Local\\Google\\Chrome\\User Data")
w = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=options)
w.get("https://m.uber.com/looking")
time.sleep(6)
w.find_element_by_xpath(f'"//*[#id="booking-experience-container"]/div/div[3]/div[2]/div/input"').send_keys("test")
Selenium just removed find_element_by_xpath() in version 4.3.0
now, it should be
driver.find_element("xpath", '//*[#id="mG61Hd"]div[1]/input')
https://github.com/SeleniumHQ/selenium/blob/a4995e2c096239b42c373f26498a6c9bb4f2b3e7/py/CHANGES
This error message...
w.get_element_by_xpath(f'"//*[#id="booking-experience-container"]/div/div[3]/div[2]/div/input"').send_keys("test")
AttributeError: 'WebDriver' object has no attribute 'get_element_by_xpath'
implies that the WebDriver object has no attribute as get_element_by_xpath().
You have to take care of a couple of things as follows:
If you look at the various WebDriver strategies to locate elements there is no method as get_element_by_xpath() and possibly you would like to replace with find_element_by_xpath()
While invoking click() on an element, Implicit Wait is no more effective and you need to replace it with Explicit Wait i.e WebDriverWait
Solution
So your effective code block will be:
from selenium import webdriver
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("user-data-dir=C:\\Users\\Zedas\\AppData\\Local\\Google\\Chrome\\User Data")
w = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=options)
w.get("https://m.uber.com/looking")
WebDriverWait(w, 20).until(EC.element_to_be_clickable((By.XPATH, f'"//*[#id="booking-experience-container"]/div/div[3]/div[2]/div/input"'))).send_keys("test")

Selenium unable to find element by ID in headless mode

I'm trying to automate email printing with Selenium & Chrome Driver; for some reason, the snippet below works fine when I exclude the options=options argument (i.e. not in headless mode). When I switch headless on, even specifying window size it still is unable to find the element by ID.
Config
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from time import sleep
#-----------------DEFAULT CONFIG
options = Options()
options.add_argument('--headless')
options.add_argument('--window-size=1920,1080')
options.add_argument("--allow-insecure-localhost")
options.add_argument("--allow-insecure-content")
options.add_argument("--no-sandbox")
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
PATH = '/Users/*********/2021/selenium/chromedriver'
driver = webdriver.Chrome(PATH,options=options)
Failing Block (last line)
google_login = 'https://accounts.google.com/Login#identifier'
driver.get(google_login)
sleep(2)
html = driver.page_source.strip()
# =============EMAIL BOX
user_name = driver.find_element_by_id('identifierId')
Error Traceback: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="identifierId"]"}
I've tried using WebDriverWait in a try/except which also doesn't help.
Screenshot captured in headless mode attached (line prior to the failing step)
see headless mode is bit fast so I would suggest you to use it with Explicit waits :
wait = WebDriverWait(driver, 20)
user_name = wait.until(EC.element_to_be_clickable((By.ID, "identifierId")))
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Refer here for more
Instead of
options.add_argument("--window-size=1920,1080")
Please try this.
options.add_argument('--window-size=1920x1080')

Selenium code that works in python/spyder does not works in colab

I am using selenium in Python and Colab.
I have some code that works in spyder, extracts elements but gives me error in collab
NoSuchElementException: Message: no such element: Unable to locate element:
What is a possible explanation and is it possible to fix this problem?
Try using this method of waiting before an element Is accessed.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
driver = webdriver.Chrome(executable_path='path')
waitshort = WebDriverWait(driver,.5)
wait = WebDriverWait(driver, 20)
waitLonger = WebDriverWait(driver, 100)
visible = EC.visibility_of_element_located
driver.get('website')
element = wait.until(visible((By.XPATH,'element_xpath'))).click()

Categories