Selenium module `NoSuchElementElement` exception [PYTHON] - python

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: #loginUsername
I used selenium module to automate the login process of some websites and when I executed the program, it throws NoSuchElementException while I tried all the find_element_by_* methods ( by id, by class, by css selector, by text, by name attr ) but, still I get this error and I don't know how to make this program error-free so, is there any other way to debug the program??

I don't have enough reputation to comment so posting an answer. I assume any one of the below reason can be the cause.
Please check the element xPath id etc. in chrome console to make sure you are using right locator
There is not enough time you are giving for element to be accessible. Please use explicit wait.
May be element is present on DOM but not visible when you try to perform action.
From error message I can see you are using css selector #loginUsername. I assume you are not giving tag name like it suppose to be input#loginUsername

I understand that there are no iframes from what you said above.
Make sure that you wait for the element to be loaded and made available before you call find_element(...).
Do something like this:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome("D:/chromedriver/94/chromedriver.exe")
driver.get("https://yourwebsite.com")
# use By.XPATH, By.CLASS_NAME etc. depending on the
# element you're referring to
WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.ID, 'loginUsername')))
login = driver.find_element(By.ID, 'loginUsername')

Related

How to clear "selenium.common.exceptions.ElementNotInteractableException" error?

I am a newbie to python so please excuse me. I have no background in programming, I am just trying things to smooth out something for me by automating.
I found other similar questions but my problem still persists after following some of the solutions posted.
I am trying to automate uploading files to a website, the website allows one file for upload at once, so I am trying to automate uploading the task of uploading one by one.
I am using selenium and I have progressed to logging in the page and attaching the file to upload. Now I am stuck as the website requires 3 tags to be inputted. There is one long field to input tags and is backed up by auto suggestion/complete. Cannot enter custom words, only those provided by website which is in auto suggestion.
The above error is displayed when I try to sendkeys() to that tag field element. Below is that element
[https://imgur.com/a/VGq7AtY]
I have tried Implicit wait and explicit wait as well but it doesn't work out, as seen below
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.get('https://www.website.com/login')
driver.find_element_by_xpath('//*[#id="app"]/div[1]/form/div[1]/div[2]/div[1]/input').send_keys('username')
driver.find_element_by_xpath('//*[#id="app"]/div[1]/form/div[1]/div[3]/div[1]/input').send_keys('password')
driver.find_element_by_xpath('//*[#id="app"]/div[1]/form/div[2]/button').click()
driver.find_element_by_xpath('/html/body/div/header/div[1]/span[2]').click() // upload page
driver.find_element_by_xpath('//*[#id="app"]/div[1]/div/div[1]/div[1]/div[1]/div[1]/label/input').send_keys('/home/userrobin/Desktop/file.html') // file to be uploaded path
time.sleep(10)
driver.implicitly_wait(40)
tags = WebDriverWait(driver, 40).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="tags-selector"]')))
# tags = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, '//*[#id="tags-selector"]')))
tags.send_keys('assignment')
Error : selenium.common.exceptions.ElementNotInteractableException: Message: Element is not reachable by keyboard
What am I missing, what can I do to identify the problem?
Thank you
EDIT
added
I tried with
`
(By.XPATH, '//input[#id="tags-selector"]')
` , it gives is invalid: SyntaxError: Document.evaluate: The expression is not a legal expression Stacktrace: , I am not sure on how to refine locator to include //input
tried
driver.find_element_by_xpath('//*[#id="tags-selector"]/input').send_keys('assignment')
but it gave selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //[#id="tags-selector"]/input*
tried
driver.find_element_by_xpath('//*[#id="tags-selector"]').click()
this works on that field as now I can see the cursor in the field.
but when I try send_keys(), it shows me error not reachable by keyboard

Python + Selenium: Unable to locate element

I'm trying to locate a tab and click it on my webpage. However, my code does not work:
ActionChains(driver).move_to_element(driver.find_element_by_xpath("//*[#id='tab_bd3ae39d-f956-49ab-b7bd-f13507de9351']/div[2]/div")).perform()
additionaldata_ele= driver.find_element_by_xpath("//*[#id='tab_bd3ae39d-f956-49ab-b7bd-f13507de9351']/div[2]/div").click()
The HTML body is as follows:
<li class="WJX1 WLV1" id="tab_015ba30c-af6c-4c9a-ac34-f77ee00805b6" role="tab" aria-controls="tabPanel_16845ddd-961b-4581-89da-a6a4e6080930" data-automation-id="tab" aria-selected="false"><div class="WGX1"></div><div class="WEX1"><div class="gwt-Label WLX1" data-automation-id="tabLabel">Additional Data</div></div></li>
I get the error -
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id='tab_bd3ae39d-f956-49ab-b7bd-f13507de9351']/div[2]/div"}
I guess the reason is that when I try to find the element, it doesn't appear on the DOM, so I should implement WebDriverWait until the element is visible. So I tried WebDriverWait, but it didn't work either.
Many thanks for all answers and replies!
second edition:
Here is the webpage, sorry I cannot share the link, it is an internal webpage and PSW is required:
This is the screenshot of the page
That id looks dynamic - it will probably change frequently and result in an unstable script.
Additionally, you will want a wait to ensure your web page is ready before you continue.
try something like this:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[text()="Additional Data"]'))).click()

Selenium no such element: Unable to locate element (trying to login)

I am trying to use Selenium to download a file from https://id.opswat.com, in order to access it I need to login, but I can't, because when I try to retrieve the email input, it gives me an error ... This is my code:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://id.opswat.com/login')
driver.find_element_by_name('email').send_keys('email#email.es')
driver.find_element_by_class_name('form--button is-primary is-fullwidth button').click()
driver.find_element_by_name('password').send_keys('pass')
driver.find_element_by_class_name('form--button is-primary is-fullwidth button').click()
And this is the error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="email"]"}
The login on that page is a bit special, because first you have to enter the email, check that it is well formed, click to continue, and you will see the password entry and the button to log in ...
Maybe something very basic is missing me, but I can't find it ... (I've tried waits, but neither ...)
First, your line
driver.find_element_by_class_name('form--button is-primary is-fullwidth button').click()
will always fail because class_name function does not handle spaces in the class. I'd suggest changing that to
driver.find_element_by_css_selector('.form--button.is-primary.is-fullwidth.button').click()
Next, You need to wait for the page to load before you try to interact with each field. This works for me:
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()
driver.get('https://id.opswat.com/login')
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.NAME, 'email'))).send_keys('email#email.es')
driver.find_element_by_css_selector('.form--button.is-primary.is-fullwidth.button').click()
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.NAME, 'password'))).send_keys('pass')
driver.find_element_by_css_selector('.form--button.is-primary.is-fullwidth.button').click()
I can't see anything obviously wrong with it, have you checked if you can get the driver.find_element_by_class_name('emailInputBox')? Or any other element in the page?
Does it work when getting by XPath?
//*[#id="root"]/div/section/div/div/div[2]/form/div[2]/div/div/div/input
You could also try waiting for the element to be present like in this answer:https://stackoverflow.com/a/50324722/10832847

Selium Unable to Locate Class Element

Dear Stackoverflowers,
I'm trying to automate a CC payment process but Selenium is having a hard time identifying a specific element I want to click on. I'm trying to click on 'REI Card - 6137' so that I can continue to the payment page. Using the inspect tool it shows the class as, "soloLink accountNamesize". Unfortunately, there's not an ID I can go after. When I try to search by class name I get this error in the console:
selenium.common.exceptions.NoSuchElementException: Message: Unable to
locate element: .soloLink accountNamesize
Below is a picture of the site and the inspector pane with the thing I'm trying to click on highlighted in blue. Since its my credit card and I'm already logged it a link to the page wouldn't really help you guys.
The script gets hung up on "driver.find_element_by_class_name('soloLink accountNamesize').click()"
My code is below:
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 yaml
import time
conf = yaml.load(open(r'D:\Users\Matt\Documents\GitHub\YML_Files\REI_Login_Credentials.yml'))
myREIUsername = conf['REILogin']['username']
myREIPassword = conf['REILogin']['password']
driver = webdriver.Firefox(
executable_path=
r'D:\Users\Matt\Documents\GitHub\Executable_Files\geckodriver.exe'
)
def login():
driver.get('https://onlinebanking.usbank.com/Auth/Login?usertype=REIMC&redirect=login&lang=en&exp=')
time.sleep(4)
driver.find_element_by_id('aw-personal-id').send_keys(myREIUsername)
driver.find_element_by_id('aw-password').send_keys(myREIPassword)
time.sleep(2)
driver.find_element_by_id('aw-log-in').click()
time.sleep(15)
make_payment()
def make_payment():
if (driver.find_element_by_class_name("accountRowLast").text) != "0.00":
driver.find_element_by_class_name('soloLink accountNamesize').click()
else:
driver.quit()
I've tried searching by Xpath and Xpath + Class with no luck. I also tried searching for this issue but its a fairly unique class so I didn't have much luck. Have any other ideas I could try?
soloLink accountNamesize is multiple class names use the following css selector instead to click on that element.
driver.find_element_by_css_selector('a.soloLink.accountNamesize').click()
To induce waits we do
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.soloLink.accountNamesize"))).click()
Based on the photo, I think that this is the xpath that you might want
//div[#id='MyAccountsDiv']//div[#id='CreditsTableDiv']//tbody//tr[#class='accountRowFirst']//a[contains(#onclick, 'OpenAccountDashboard')]
As you can see, this xpath starts off with the top-most div that might be unique ( MyAccountsDiv ) and continues to dive into the HTML code.
Based off of this, you could click on the link with the following code
xpath = "//div[#id='MyAccountsDiv']//div[#id='CreditsTableDiv']//tbody//tr[#class='accountRowFirst']//a[contains(#onclick, 'OpenAccountDashboard')]"
driver.find_element(By.XPATH, xpath).click()
NOTE
Your error says
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="aw-personal-id"]
Maybe you can use the above technique and see if you can isolate the xpath for the web element instead.

Unable to locate element within an iframe through Selenium

I'm new to Selenium. I'm trying to write a Python script that will log in to a particular form. The form is located at http://www.tvta.ca/securedContent
The code I'm running is:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://tvta.ca/securedContent/")
elem = driver.find_element_by_name("txtUserName")
elem.clear()
elem.send_keys("<<my email>>")
I get an error that says:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [name="txtUserName"]
I'm not sure what I'm missing here? I've checked the source of the webpage the login field is definitely named txtUserName.
You need to switch to the frame to write text in the textbox, try to check syntax once as I have less in good in Python
framLogin= driver.find_element_by_id("membeeLoginIF")
driver.switch_to.frame(framLogin)
EmailTxt = driver.find_element_by_name("txtUserName");
EmailTxt.send_Keys("Test#gmail.com");
Same in Java
WebElement framLogin= driver.findElement(By.id("membeeLoginIF"));
driver.switchTo().frame(framLogin);
WebElement EmailTxt = driver.findElement(By.name("txtUserName"));
EmailTxt.sendKeys("Test#gmail.com");
That site requires third-party cookies to be enabled; without them, the login form does not load. It's likely you have your browser configured that way but the defaults for webdriver.Firefox do not.
To see what Selenium is actually seeing, dump driver.page_source and/or take a screenshot with driver.save_screenshot(...).
The desired element is within an <iframe>. So as per best practices you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable and you can use the following Locator Strategies:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"membeeLoginIF")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.textboxWaterMark#txtUserName"))).send_keys("Jeff")
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

Categories