Message: Unable to locate element, Selenium Python - python

I'm trying to get access to this page "fullcollege" with a bot I'm making for students. The problem is that I can't even select an element from it, and this error shows up. I have recently tested my code with another webpages like instagram and everything works perfectly. Anyone knows what can I do to solve this? Thanks in advance.
My code:
from time import sleep
from selenium import webdriver
browser = webdriver.Firefox()
browser.implicitly_wait(5)
browser.get('https://www.fullcollege.cl/fullcollege/')
sleep(5)
username_input = browser.find_element_by_xpath("//*[#id='textfield-3610-inputEl']")
password_input = browser.find_element_by_xpath("//*[#id='textfield-3611-inputEl']")
username_input.send_keys("username")
password_input.send_keys("password")
sleep(5)
browser.close()
The error:
Traceback (most recent call last):
File "c:\Users\marti\OneDrive\Escritorio\woo\DiscordBot\BetterCollege\tester.py", line 11, in <module>
username_input = browser.find_element_by_xpath("//*[#id='textfield-3610-inputEl']")
File "C:\Users\marti\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\marti\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\marti\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\marti\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //*[#id='textfield-3610-inputEl']

The username and password field is inside and iframe you need to switch it first.
browser.get('https://www.fullcollege.cl/fullcollege/')
WebDriverWait(browser,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#logFrame")))
sleep(5)
username_input = browser.find_element_by_xpath("//input[#id='textfield-3610-inputEl']")
password_input = browser.find_element_by_xpath("//input[#id='textfield-3611-inputEl']")
username_input.send_keys("username")
password_input.send_keys("password")
import below libraries as well.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

Related

python with selenium automating login

I'm new to selenium
Here I want to ask about a problem code (actually not mine)
this is the code
aww = email.strip().split('|')
driver = webdriver.Chrome()
driver.get("https://stackoverflow.com/users/signup?ssrc=head&returnurl=%2fusers%2fstory%2fcurrent")
time.sleep(5)
loginform = driver.find_element_by_xpath("//button[#data-provider='google']")
loginform.click()
mailform = driver.find_element_by_id('identifierId')
mailform.send_keys(aww[0])
driver.find_element_by_xpath("//div[#id='identifierNext']").click()
time.sleep(3)
passform = driver.find_element_by_css_selector("input[type='password']")
passform.send_keys(aww[1])
driver.find_element_by_id('passwordNext').click()
time.sleep(3)
driver.get("https://myaccount.google.com/lesssecureapps?pli=1")
open('LIVE.txt', 'a+').write(f"CHECKED : {aww[0]}|{aww[1]}")
time.sleep(3)
lessoff = driver.find_element_by_xpath('//div[#class="hyMrOd "]/div/div/div//div[#class="N9Ni5"]').click()
driver.delete_all_cookies()
driver.close()
I'm using those code for automating turn on the less-secure apps from Gmail
and the error will pop up like this
quote Traceback (most recent call last):
File "C:\Users\ASUS\Downloads\ok\less.py", line 59, in
lessoff = driver.find_element_by_xpath('//div[#class="hyMrOd "]/div/div/div//div[#class="N9Ni5"]').click()
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class="hyMrOd "]/div/div/div//div[#class="N9Ni5"]"}
(Session info: chrome=86.0.4240.183)
any help gonna be helpfull,sorry for my english before :)
You can simply target this xpath and .click to toggle the less secure apps.
lessoff = driver.find_element_by_xpath("input[type='checkbox']").click()
It looks like it couldn't find the element it's looking for so give some time to load the element. You can check with Wait().until.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
wait(driver, 10).until(EC.presence_of_element_located((By.XPATH, 'YOUR_XPATH')))
when you try to click an element make sure it's there. above code will wait until the element located for the 10s if the element not located then it will throw an exception

Python Selenium - Unable to locate element

I just started learning selenium and I'm facing some issues with some code that should be quite easy to write..
I'm simply trying to enter "Paris" in the input field, but I keep getting the error: "Unable to locate element". Do I need to refer to the div tag or the input tag in order for it to work?
Here's my code at the moment.
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 as wait
from selenium.webdriver.support import expected_conditions as EC
import time
# Setup webdriver
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
# Open website maximized
driver.get("https://www.corsair.ca/")
driver.maximize_window()
print(driver.title)
# Select and enter destination
wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Votre destination']"))).click()
to_city = driver.find_element_by_class_name("form-control valid")
to_city.send_keys("Paris")
to_city.send_keys(Keys.RETURN)
Here's the error I have
Traceback (most recent call last):
File "C:\Users\Admin\PycharmProjects\FlightFinder\venv\SeleniumTutorial.py", line 26, in <module>
to_city = driver.find_element_by_class_name("form-control valid")
File "C:\Users\Admin\PycharmProjects\FlightFinder\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 564, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "C:\Users\Admin\PycharmProjects\FlightFinder\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\Admin\PycharmProjects\FlightFinder\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Admin\PycharmProjects\FlightFinder\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".form-control valid"}
(Session info: chrome=86.0.4240.183)

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [name="username"]

I'm pretty new to Python and Selenium. I was following a tutorial on how to code an Instagram bot to gain followers and likes.
The program opens a Firefox window as it should, but then when it has to fill in the username and password field it does nothing. Then when i close down the window i find myself with this error.
It's really odd that i get this error because i reviewed the elements on https://www.instagram.com/accounts/login/ and in the username field there was a name='username' as well as an name='password' in the password field.
Can anybody help me out?
This is a piece of the code:
class InstagramBot:
def __init__(self, username, password):
self.username = username
self.password = password
self.driver = webdriver.Firefox()
self.login()
def login(self):
self.driver.get('https://www.instagram.com/accounts/login/')
self.driver.find_element_by_name('username').send_keys(self.username)
self.driver.find_element_by_name('password').send_keys(self.password)
This is the error message.
Traceback (most recent call last):
File "C:/Users/Python Programming/PycharmProjects/insta_bot/bot.py", line 25, in <module>
ig_bot = InstagramBot('temp_username', 'temp_password')
File "C:/Users/Python Programming/PycharmProjects/insta_bot/bot.py", line 14, in __init__
self.login()
File "C:/Users/Python Programming/PycharmProjects/insta_bot/bot.py", line 19, in login
self.driver.find_element_by_name('username').send_keys(self.username)
File "C:\Users\Python Programming\PycharmProjects\insta_bot\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 496, in find_element_by_name
return self.find_element(by=By.NAME, value=name)
File "C:\Users\Python Programming\PycharmProjects\insta_bot\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\Users\Python Programming\PycharmProjects\insta_bot\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Python Programming\PycharmProjects\insta_bot\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [name="username"]
It appears that you may need to wait for some scripts on the page to finish before that element exists.
here are the import statements you'll need:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Use this code to wait for that element before your send_keys line:
WebDriverWait(self.driver,10).until(
EC.presence_of_element_located((By.NAME, "username")))

selenium python doesnt login into social media

So im pretty new to selenium and im following the docs to make some bots,
but when i try to login into social media networks (twitter/instagram) it doesnt send the strokes.
Code:
#!usr/bin/env python3
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Firefox()
browser.get("https://www.instagram.com/accounts/login/")
elem = browser.find_element_by_name("username")
elem.send_keys('Laptops' + Keys.RETURN)
time.sleep(4)
browser.quit()
i've tried it by using- browser.get_element_by_name/class/xpath but nothing seemed to work.
Error code:
Traceback (most recent call last):
File "ig.py", line 50, in <module>
login(driver)
File "ig.py", line 15, in login
driver.find_element_by_xpath("//div/input[#name='username']").send_keys(username)
File "/home/lario/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 365, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/home/lario/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 843, in find_element
'value': value})['value']
File "/home/lario/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 308, in execute
self.error_handler.check_response(response)
File "/home/lario/.local/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //div/input[#name='username']
os=ubuntu17
driver=firefox/geckodriver
python3.6
selenium3.6
I know this code shouldnt work bc you need a password and a username, but it doesnt even execute the send_keys code because of the error on the line above tHAT
I've had some problems with Firefox driver on linux too.
I copied your code and only changed the driver to Chrome and it worked.
You can get the Chrome driver here:
http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.chrome.webdriver
Try adding a wait-for presence condition. It may however require a EC.element_to_be_clickable, if the element is disabled until some check is performed.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
...
usernamefield = WebDriverWait(self.driver, 10)\
.until(EC.presence_of_element_located((By.NAME, 'username')))
usernamefield.send_keys("Laptops")
passwordfield = WebDriverWait(...

UnovaRPG automator: Selenium Python image error

I am trying to make a python program using Selenium to automate the UnovaRPG game. It is supposed to do the following in order:
1) Login with username and password.
2) Go to heal center and click heal.
3) Fight the trainer "shedinja144" (using given link)
4) Select a specified pokemon.
5) During the fight, it should click the last attack followed by continue.
6) Then click "Back to map" for the last pokemon (The 6th).
7) Repeat steps 2-5 any given number of times.
Here is my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("https://www.unovarpg.com/login.php")
assert "UnovaRPG" in driver.title
i=1
j=1
typeUsername = driver.find_element_by_id("username")
typePassword = driver.find_element_by_id("password")
typeUsername.send_keys("******")
typePassword.send_keys("******")
driver.find_element_by_id("buttonLogin").click()
while i<10:
driver.get("https://www.unovarpg.com/pokemon_center.php")
driver.find_element_by_id("healButton").click()
driver.get("https://www.unovarpg.com/battle.php?type=autotrainer&tid=5546527")
driver.find_element_by_xpath("/html/body/div[3]/div[2]/div[3]/div/div/div[2]/div[2]/div[8]/div[4]/div[1]/img").click()
while j<6:
driver.find_element_by_xpath("/html/body/div[3]/div[2]/div[3]/div/div/div[2]/div[2]/div[4]/div[6]/div[2]/ul/li[4]/a/strong").click()
driver.find_element_by_id("continue").click()
j+=1
driver.find_element_by_xpath("/html/body/div[3]/div[2]/div[3]/div/div/div[2]/div[2]/div[4]/div[6]/div[2]/ul/li[4]/a/strong").click()
driver.find_element_by_id("Back2MapButton").click()
i+=1
Here is the error:
Traceback (most recent call last):
File "Selenium v1.py", line 21, in <module>
driver.find_element_by_xpath("/html/body/div[3]/div[2]/div[3]/div/div/div[2]/div[2]/div[8]/div[4]/div[1]/img").click()
File "E:\Programming\Python\Portable Python 2.7.6.1\App\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 230, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "E:\Programming\Python\Portable Python 2.7.6.1\App\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 662, in find_element
{'using': by, 'value': value})['value']
File "E:\Programming\Python\Portable Python 2.7.6.1\App\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 173, in execute
self.error_handler.check_response(response)
File "E:\Programming\Python\Portable Python 2.7.6.1\App\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 166, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"/html/body/div[3]/div[2]/div[3]/div/div/div[2]/div[2]/div[8]/div[4]/div[1]/img"}
Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///c:/users/adb/appdata/local/temp/tmpo1kmq3/extensions/fxdriver#googlecode.com/components/driver-component.js:9641:26)
at FirefoxDriver.prototype.findElement (file:///c:/users/adb/appdata/local/temp/tmpo1kmq3/extensions/fxdriver#googlecode.com/components/driver-component.js:9650:3)
at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/adb/appdata/local/temp/tmpo1kmq3/extensions/fxdriver#googlecode.com/components/command-processor.js:11635:16)
at DelayedCommand.prototype.executeInternal_ (file:///c:/users/adb/appdata/local/temp/tmpo1kmq3/extensions/fxdriver#googlecode.com/components/command-processor.js:11640:7)
at DelayedCommand.prototype.execute/< (file:///c:/users/adb/appdata/local/temp/tmpo1kmq3/extensions/fxdriver#googlecode.com/components/command-processor.js:11582:5)
***Repl Closed***
The error lies in the inability to find the image of the pokemon to click (Step 4). I have used the xcode path copied from the Firebug inspector.
Help would be much appreciated.
Please note that this is just for creative purposes and will not be used to "cheat".
You need to explicitly wait for the element to load before interacting with it:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "/html/body/div[3]/div[2]/div[3]/div/div/div[2]/div[2]/div[8]/div[4]/div[1]/img"))
)

Categories

Resources