Selenium: entering Username with find_element_by_name, how to locate Name? - python-3.x

I'm using a bit of code from a related question
username = driver.find_element_by_name("username")
username.send_keys("username")
I looked at the source code for the login page, and saw:
HTML snippet
However, I'm getting errors for both
username = driver.find_element_by_name("Email")
and
username = driver.find_element_by_name("ember442")
The error says:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/anaconda/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 385, in find_element_by_name
return self.find_element(by=By.NAME, value=name)
File "/anaconda/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 791, in find_element
'value': value})['value']
File "/anaconda/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 256, in execute
self.error_handler.check_response(response)
File "/anaconda/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"Email"}
What am I doing wrong here? My only other thought would be to use one of the other commands for locating elements (find_element_by_id,
find_element_by_name,
find_element_by_xpath,
find_element_by_link_text,
find_element_by_partial_link_text,
find_element_by_tag_name,
find_element_by_class_name,
find_element_by_css_selector) but those don't seem to work either.

It's because none of those are element names. You should use :
username = driver.find_element_by_id("ember442")

Here is the Answer to your Question:
For Email try xpath as //input[#id='ember442']:
username = driver.find_element_by_xpath("//input[#id='ember442']")
username.send_keys("username")
For Password try xpath as //input[#id='ember443']:
username = driver.find_element_by_xpath("//input[#id='ember443']")
username.send_keys("username")
Let me know if this Answers your Question.

Got it! I ended up finding a related question (albeit one using Java) and a piece of code in an answer: driver.findElement(By.xpath("//input[#name='name']")) and modified it to read: username = driver.find_element_by_xpath("//input[#placeholder='Email']"‌​)
I'd love to know how to do this by referencing the id name (find_element_by_id) but this was a task-specific question so it'll work for now!
TLDR; I had to specify the placeholder name within find_element_by_xpath.

Related

KeyError: 'document' occures when I try to use execute_script command in Python Selenium

I have simple code. Firstly I get an input from user. In target page, there is an editable div. I focus on it and insert the message that I got from user. When I focusing and inserting element I use Javascript so for using Javascript with Python Selenium I use execute_script command.
Code:
message = input()
message = message.replace("'","\\'")
message = message.replace('"','\\"')
browser.execute_script("setTimeout(function() {document.querySelector('._3ee1T ._3uMse ._2FVVk ._3FRCZ').focus();}, 0); document.querySelector('._3ee1T ._3uMse ._2FVVk ._3FRCZ').textContent = '{}'".format(message))
Error: Traceback (most recent call last): File "main.py", line 171, in <module> browser.execute_script("setTimeout(function() {document.querySelector('._3ee1T ._3uMse ._2FVVk ._3FRCZ').focus();}, 0); document.querySelector('._3ee1T ._3uMse ._2FVVk ._3FRCZ').textContent = '{}'".format(message)) KeyError: 'document'

Selenium python can't send keys to second input field

I'm trying to fill in a webform with selenium and python.
I'm looping trough all input fields and sending each a "Test" string.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('C:/Users/kbu/chromedriver.exe')
driver.get('https://skovbakken.halbooking.dk/proc_error.asp')
all_inputs = driver.find_elements_by_xpath("//input[#class='textbox_login']")
for input in all_inputs:
input.send_keys("Test")
Only the first is filled out. Why is that?
This is the error i get:
Traceback (most recent call last):
File "C:\Users\kbu\Dropbox\Python\squash.py", line 10, in <module>
input.send_keys("Test")
File "C:\Users\kbu\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webelement.py", line 477, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT,
File "C:\Users\kbu\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Users\kbu\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\kbu\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.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=84.0.4147.125)
Here is the HTML:
The Locator Strategy which you have used:
driver.find_elements_by_xpath("//input[#class='textbox_login']")
identifies exactly 3 elements within the HTML.
The first and last elements are the username and password field respectively.
But the second matching element contains the style attribute value as style="DISPLAY: none;:
<input onkeydown="CheckEnterNoIE('logon', event.keyCode)" name="mf_nnpg0fjm" type="password" id="mf_nnpg0fjm" onfocus="if(this.value==multiform.loginvar6.value){this.value='';}" maxlength="50" tabindex="1" class="textbox_login" style="DISPLAY: none;width:120px;">
Hence when your script tries to invoke send_keys() on it you face ElementNotInteractableException.
Reference
You can find a couple of relevant discussion on ElementNotInteractableException in:
Element not Interactable when using the selenium function find element by name but works when using find element by xpath method
ElementNotInteractableException: Message: element not interactable error sending text in search field using Selenium Python

How to Handle Popup Windows That Occur within Selenium Python

So I have an issue where, I am trying to automate an Import on an application that has no API.
As a result, I have to do like 30 navigation clicks just to get to what I want (Exaggeration).
However, I am trying to basically automate the clicks that will allow me to upload a specific file.
As a result, I almost get to the part where I have to select the specific test build I want to import the file with. There is a field that I need to do a send_keys to find the correct import build I have to upload. The Field element looks like this
<input class="lookupInput" type="text" name="brTestScoreImportLookupInput" id="brTestScoreImportLookupInput" style="width: 100px;" tabindex="1" onkeydown="return lookupKeyPressed(event,"","simptbrws000.w")" origvalue="" det="true" aria-labelledby="" autocomplete="off">
However I don't think my code is properly handling the window as it pops-up from the prior selection.
The field I need to update can be found in the picture I uploaded:
Furthermore the XPATH for the Field is //*[#id='brTestScoreImportLookupInput']
You can find the full code here.
The main aspect is I have to Enter TSI into that File ID field and then hit enter on my keyboard to populate the correct import utility I need. Once I do that the import utilities filter out and I need to select a specific File ID:
.
The main code that should be controlling this:
# Click on Test Score Import Wizard - TW
# Test Wizard XPATH = //a[#id='tree1-3-link']/span
element = WebDriverWait(browser, 20).until(
EC.element_to_be_clickable((By.XPATH, "//a[#id='tree1-3-link']/span")))
element.click();
# Send test_upload and Send Keys
# Field XPATH = //*[#id='brTestScoreImportLookupInput']
test_lookup = browser.find_element_by_id("brTestScoreImportLookupInput")
test_lookup.send_keys(test_upload)
If you want to visit the link toe repository code click on here above this.
Any help would be greatly appreciated.
Traceback (most recent call last): File ".\skyward_collegeboard_TSI_import.py", line 115, in
<module> test_lookup = browser.find_element_by_id("brTestScoreImportLookupInput") File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id return self.find_element(by=By.ID, value=id_) File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py",
line 976, in find_element return self.execute(Command.FIND_ELEMENT, { File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response) File "C:\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: no such element: Unable to locate element: {"method":"css selector","selector":"[id="brTestScoreImportLookupInput"]"}
(Session info: chrome=80.0.3987.122)
So I was able to accomplish this by using the following method using both selenium and pynput.
# Browser Switches to Window
WebDriverWait(browser,10).until(EC.number_of_windows_to_be(2))
browser.switch_to.window(browser.window_handles[-1])
# Send test_upload and oend Keys
# Field XPATH = //*[#id='brTestScoreImportLookupInput']
test_lookup = browser.find_element_by_id("brTestScoreImportLookupInput")
test_lookup.send_keys(test_upload)
# Press and Release Enter Key
keyboard.press(Key.enter)
keyboard.release(Key.enter)
Essentially I had to switch to that popup window.

CherryPy encoding: bool object not iterable

Hello,
I am using CherryPy to host the gui of an application that takes json files from qualtrics and drops them in a mysql server.
The code seems to work for most surveys but for some I get the following error:
Traceback (most recent call last):
File "C:\Users\jam66\AppData\Local\Programs\Python\Python37-32\lib\site-packages\cherrypy\_cprequest.py", line 627, in respond
self._do_respond(path_info)
File "C:\Users\jam66\AppData\Local\Programs\Python\Python37-32\lib\site-packages\cherrypy\_cprequest.py", line 686, in _do_respond
response.body = self.handler()
File "C:\Users\jam66\AppData\Local\Programs\Python\Python37-32\lib\site- packages\cherrypy\lib\encoding.py", line 264,
in __call__ct.params['charset'] = self.find_acceptable_charset()
File "C:\Users\jam66\AppData\Local\Programs\Python\Python37-32\lib\site- packages\cherrypy\lib\encoding.py", line 173, in find_acceptable_charset
if encoder(self.default_encoding):
File "C:\Users\jam66\AppData\Local\Programs\Python\Python37-32\lib\site-packages\cherrypy\lib\encoding.py", line 114, in encode_string
for chunk in self.body:
TypeError: 'bool' object is not iterable
Any help on beginning to understand this issues is appreciated
My guess is that some of your exposed methods are returning a boolean. You have to return a string or an iterable. Unless you are using the json tool, in that case the dictionary to string is handled by the tool.
As a way to debug it, just print or log the value that would will be returned, verify the type with the type function.

ldap3/python3 Connection modify() function error(malformed change) with active directory

i have an active directory running and would like to change an attribute of an user. Im using the python3 ldap3 library and the modify() method.
My code below trows an exception every time i try to modify the user.
from ldap3 import Server, Connection, MODIFY_REPLACE
server = Server('ldaps://domain.local:636', port = 636, use_ssl = True)
try:
conn = Connection(server, user="philipp#domain.local", password="MYPW", auto_bind=True)
print("User found")
except:
print("User not found or connection error")
result = conn.modify('CN=phil stone,OU=Firewall,DC=domain,DC=local',{'rules': [(MODIFY_REPLACE, ['replaced-text'])]})
print(result)
Traceback (most recent call last):
File "/mnt/hgfs/shared/eclipse/osfipa/ldap/ldap_write.py", line 28, in <module>
result = conn.modify('CN=phil stone,OU=Firewall,DC=domain,DC=local',{'rules': [(MODIFY_REPLACE, ['replaced-text'])]})
File "/usr/local/lib/python3.5/dist-packages/ldap3/core/connection.py", line 763, in modify
raise LDAPChangesError(self.last_error)
ldap3.core.exceptions.LDAPChangesError: malformed change
In the AD GUI i have checked the Attribute "distinguishedName" of the User(Object) and its Value is "CN=philipp stone,OU=Firewall,DC=domain,DC=local".
So the DN for the modify function should be correct. And i want to change the "rules" attribute.
Any Ideas what im doing wrong here?
Thx in advance.
https://github.com/cannatag/ldap3/issues/42
There is a simple syntax error. In the above link one can finde the answer.
The simple solution was to change it like the code below.
result = conn.modify('CN=phil stone,OU=Firewall,DC=domain,DC=local',{'rules': (MODIFY_REPLACE, ['replaced-text'])})
Look at the part "MODIFY_REPLACE replaced-text", there is no list([]) around it;)

Resources