Python Selenium - Unable to locate element - python

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)

Related

Message: Unable to locate element, Selenium 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

How to input a text on a textbox using selenium with Python

I'm using Selenium with Python to input an address into a textbox within the Glovo page for Madrid. The code I wrote can be seen below, and the error I get is also copied after the 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
import time
url = 'https://glovoapp.com/es/es/madrid/'
# open browser
driver = webdriver.Chrome()
# load page
driver.get(url)
# find field
item = driver.find_element_by_class_name('address-input__container')
# select textbox and input text
time.sleep(2)
item.click()
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "el-input__inner"))
)
item = driver.find_element_by_class_name('el-input__inner')
time.sleep(2)
item.send_keys('Calle')
The error I get is shown below.
Traceback (most recent call last):
item.send_keys('Calle')
File "C:\Users\Usuario\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 477, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT,
File "C:\Users\Usuario\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Users\Usuario\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Usuario\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
`ElementNotInteractableException: element not interactable`
(Session info: chrome=91.0.4472.124)
Locator you are using is returning 2 different elements. See if this works
item = driver.find_element_by_xpath("//input[#class='el-input__inner' and #data-test-id='address-input-autocomplete']")
item.send_keys("Calle")
Using various xpath for the element address-input__containerand trying to click it is not working while automation and throws ElementNotInteractableException: Message: element not interactable You can refer this question. Applying the same and it works.
from selenium.webdriver.common.action_chains import ActionChains
driver.get("https://glovoapp.com/es/es/madrid/")
box = driver.find_element_by_class_name('address-input__container')
time.sleep(2)
ActionChains(driver).move_to_element(box).click(box).perform()
driver.find_element_by_xpath("//input[#data-test-id='address-input-autocomplete']").send_keys("SampleText")

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

Can't click a subutton in a pop-up

I have a problem with selenium:
I'm not able to click a button that is included in a pop-up originated by the first button that I click.
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
from selenium.webdriver.firefox.options import Options
# Webdriver
browser = webdriver.Chrome('C:\\Users\\zlell\\PycharmProjects\\PyroLello\\Userbot\\chromedriver.exe')
browser.get("https://www.eventbrite.com/e/120621788015")
# This button originates a pop-up
python_button = browser.find_element_by_xpath('//*[#id="eventbrite-widget-modal-trigger-120621788015"]')
# Click
python_button.click()
# The pop-up with the new button appears
# Try to find the button included in the pop-up - Doesn't work
python_button_2 = browser.find_element_by_css_selector("button.eds-btn.eds-btn--button.eds-btn--fill")
# Click - Doesn't Work
python_button_2.click()
The error:
// TRACEBACK:
Message: no such element: Unable to locate element: {"method":"css selector","selector":"button.eds-btn.eds-btn--butt
on.eds-btn--fill"}
(Session info: chrome=86.0.4240.111)
Traceback (most recent call last):
File "C:\Users\zlell\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-pack
ages\Python38\site-packages\pyrogram\dispatcher.py", line 208, in handler_worker
await handler.callback(self.client, *args)
File "C:\users\zlell\PycharmProjects\PyroLello\Userbot\pyro.py", line 318, in test
python_button_2 = browser.find_element_by_css_selector("button.eds-btn.eds-btn--button.eds-btn--fill")
File "C:\Users\zlell\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-pack
ages\Python38\site-packages\selenium\webdriver\remote\webdriver.py", line 598, in find_element_by_css_selector
return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
File "C:\Users\zlell\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-pack
ages\Python38\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\zlell\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-pack
ages\Python38\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\zlell\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-pack
ages\Python38\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":"button.eds-btn.eds-btn--button.eds-btn--fill"}
(Session info: chrome=86.0.4240.111)
Second Button - Included in the pop-up:
Add a wait after page load. Grab the iframe and switch to it.
iframe = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.TAG_NAME, "iframe")))
browser.switch_to.frame(iframe)
python_button_2 =WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "button.eds-btn.eds-btn--button.eds-btn--fill")))
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Python with Selenium: Unable to find element to type in

I'm trying to find an element on website using Selenium but am having a hard time:
The page I'm looking at: https://corporate.axisbank.co.in/
The HTML from the selected element looks as follows:
<input name="FORM_CORPORATEID" type="text" onpaste="return false" ondrop="return false" ondrag="return false" oncopy="return false" oncut="return false" autocomplete="off" id="corporateId" class="cm-input-box has-gradient-border">
I've tried using Id, XPath etc but keep getting errors:
from selenium import webdriver
driver = webdriver.Chrome()
bankurl= "https://corporate.axisbank.co.in/"
driver.get(bankurl)
driver.find_element_by_name("FORM_CORPORATEID").send_keys("USERNAME")
I end up getting the following error:
Traceback (most recent call last):
File "C:/Users/alwyn.kotze/PycharmProjects/AlwynAuto1/Automations/Axis1.py", line 8, in <module>
driver.find_element_by_name("FORM_CORPORATEID").send_keys("USERNAME")
File "C:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 489, in find_element_by_name
return self.find_element(by=By.NAME, value=name)
File "C:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 957, in find_element
'value': value})['value']
File "C:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 314, in execute
self.error_handler.check_response(response)
File "C:\Python\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":"name","selector":"FORM_CORPORATEID"}
(Session info: chrome=66.0.3359.181)
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 10.0.16299 x86_64)
What could be causing this error?
As per the HTML you have provided to find the element and send the text USERNAME you need to induce WebDriverWait for the element to be clickable as follows:
Code Block:
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()
bankurl= "https://corporate.axisbank.co.in/"
driver.get(bankurl)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#corporateId"))).send_keys("USERNAME")
Browser Snapshot:
from selenium import webdriver
import time
driver = webdriver.Chrome()
bankurl= "https://corporate.axisbank.co.in/"
driver.get(bankurl)
time.sleep(5)
driver.find_element_by_id('corporateId').send_keys("USERNAME")

Categories

Resources