How can I send keys to a hidden password box - python

I am trying to login to a site www.bet365.com
I am able to send the login information for the username, however the password box is split into both a visible and hidden element for security.
How am I able to send the password to the box in order to automate login?
I have tried accessing the visible element however when passed not all characters of the password are received.
When attempting to pass to the hidden element no password is visibly sent.
I have also tried clicking the visible element before sending but this also did not work.
wait=WebDriverWait(driver,3)
userele=wait.until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR,'div.hm-Login_UserNameWrapper input.hm-Login_InputField[type="text"]')))
userele.send_keys('xyz#gmail.com')
passwdele=wait.until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR,'div.hm-Login_PasswordWrapper input.hm-Login_InputField.Hidden[type="password"]')))
passwdele.send_keys('xxxxxxxxxxx')
btnelement=wait.until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR,'div.hm-Login_PasswordWrapper button.hm-Login_LoginBtn')))
btnelement.click()
Here is the main code for the elements on the bet365 page
<div class="hm-HeaderModule_UserAdmin ">
<div class="hm-Login ">
<div class="hm-Login_UserNameWrapper ">
<input type="text" class="hm-Login_InputField ">
<div class="hm-Login_InputText ">Join Now</div></div>
<div class="hm-Login_PasswordWrapper ">
<input type="text" class="hm-Login_InputField ">
<input type="password" class="hm-Login_InputField Hidden ">
<button tabindex="0" class="hm-Login_LoginBtn ">GO</button>
<div class="hm-Login_InputText ">Lost Login?</div></div></div></div>
Only the username passing currently works.

Well, you can send the password in visible input like this :
Code :
driver = webdriver.Chrome(executable_path = r'chromedriverPath')
wait = WebDriverWait(driver,10)
driver.maximize_window()
driver.get("https://www.bet365.com/#/HO/")
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH,"//div[#title='New Customer Bonus']"))).click()
userele = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div.hm-Login_UserNameWrapper input.hm-Login_InputField[type='text']")))
userele.send_keys('xyz#gmail.com')
passwdele = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div.hm-Login_PasswordWrapper input.hm-Login_InputField[type='text']")))
passwdele.send_keys('xxxxxxxxxxx')
btnelement = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'div.hm-Login_PasswordWrapper button.hm-Login_LoginBtn')))
btnelement.click()
imports would be :
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
Let me know, if you are facing any problem.

You need to avoid invoking send_keys() to a hidden password box in all possible ways. However inducing WebDriverWait for the first element on the page you want to interact would be enough and you don't need to invoke WebDriverWait multiple times when interacting with <input> elements. You can try either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.hm-Login_UserNameWrapper>input.hm-Login_InputField[type='text']"))).send_keys('xyz#gmail.com')
driver.find_element_by_css_selector("div.hm-Login_PasswordWrapper>input.hm-Login_InputField:not(.Hidden)").send_keys("hello")
Using XPATH:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='hm-Login_UserNameWrapper ']/input[#class='hm-Login_InputField ' and #type='text']"))).send_keys('xyz#gmail.com')
driver.find_element_by_xpath("//div[#class='hm-Login_PasswordWrapper ']/input[contains(#class, 'hm-Login_InputField') and not(#class='Hidden')]").send_keys("Jp1875")
Note : 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

Related

How to click on checkbox filter with selenium Python?

I'm trying to click on a checkbox filter on a website with selenium python. This is a part of its HTML code linked to one of the checkbox options.
<div class="shopee-checkbox" bis_skin_checked="1">
<label class="shopee-checkbox__control">
<input type="checkbox" name="" value="Jabodetabek">
<div class="shopee-checkbox__box" bis_skin_checked="1">
<i> </i>
</div>
<span class="shopee-checkbox__label">Jabodetabek</span>
</label>
</div>
I tried following, but it didn't work.
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('https://shopee.co.id/search?keyword=baju%20laki-laki')
time.sleep(5)
driver.find_element(By.XPATH, "//input[#value='JABODETABEK']").click()
I read the answers to similar questions, mostly they suggest using 'id' and 'name' to find the element. However, in this input tag, there are only 'type' and 'value'.
Here is one way to select that checkbox:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
[...]
wait = WebDriverWait(driver, 25)
[..]
wait.until(EC.element_to_be_clickable((By.XPATH, '//span[text()="Jabodetabek"]'))).click()
print('clicked on Jabodetabek')
Selenium documentation can be found here.
#value for the checkbox in the xpath is incorrect. The selenium web locator techniques are case sensitive. So make sure to use exact values while identifying objects. Changing your xpath to below would fix the issue.
driver.find_element(By.XPATH, "//input[#value='Jabodetabek']").click()

Python Selenium "element not interactable" error message

I'm new to Selenium and trying to automate entering data. Im trying to grab the ID and then click the textbox to send data, but i keep getting an error message. I tried the Xpath also but it doesn't seem to be working.
here is my code.
product = driver.find_element_by_id("(improved-inventory/js/extension-providers/ItemComboBox_0)[2]")
product.click()
product.send_keys("027459087093")
product.send_keys(Keys.RETURN)
Any Help would be Appreciated. Here is the HTML im currently taking the id from the input Class. When i called product.isdisplayed() it prints false.
<div class="dijitInline dijitTextBox dijitComboBox quickfill qfComboBox dijitValidationTextBox"
id="widget_improved-inventory/js/extension-providers/ItemComboBox_0"
role="combobox" aria-haspopup="true" data-dojo-attach-point="_popupStateNode" widgetid="improved-inventory/js/extension-providers/ItemComboBox_0"
aria-disabled="false" aria-owns="improved-inventory/js/extension-providers/ItemComboBox_0_popup" > == $0
<input class="dijitReset dijitInputInner" type="text" autocomplete="off"
data-dojo-attach-point="textbox,focusNode" role ="textbox" placeholder="Enter Text"
tabindex="0" id="improved-inventory/js/extension-providers/ItemComboBox_0" value aria-label="Enter Text:" aria-invalid="false" aria-disabled="false">
Try waiting until input field is clickable:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
wait = WebDriverWait(driver, timeout=30)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".dijitReset.dijitInputInner")))
product = driver.find_element_by_css_selector(".dijitReset.dijitInputInner")
product.click()
product.send_keys("027459087093")
product.send_keys(Keys.RETURN)
Also, fix your locator.

Why can I find this text box element in selenium?

Hi I cant get my selenium code to find this element in a page, it appears after ive used code to click a button on a page which opens up a form under the button.
Im looking to try get it to click on this input text box
<input id="number" class="iField" type="text" name="number">
My code
driver1.implicitly_wait(10)
driver1.until(EC.visibility_of_element_located(By.CSS_SELECTOR("input#number")))
button = driver1.find_element_by_xpath('//*[#id="number"]')
Website code
<form id="form" action="https://secureacceptance.cybersource.com/silent/embedded" method="post" autocomplete="off">
<div class="form-row">
<div class="left-col">
<label>No: </label>
</div>
<div class="right-col">
<input id="number" class="iField" type="text" name="number">
</div>
</div>
I've tried using Xpath, name Etc, but keep getting the unable to locate element.
Edit
ive also tried this
num = WebDriverWait(driver1, 10).until(EC.visibility_of_element_located(By.XPATH("//*[#id='number']")))
num.click()
But get this error
Traceback (most recent call last):
File "c:\Users\OneDrive\coding\code.py", line 68, in
num = WebDriverWait(driver1, 10).until(EC.visibility_of_element_located(By.XPATH("//*[#id='number']")))
TypeError: 'str' object is not callable
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
wait = WebDriverWait(driver, 30)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#number")))
element = driver.find_element_by_css_selector("#number")
element.click()
You have id, so you can also use find_element_by_id instead of find_element_by_css_selector:
element = driver.find_element_by_id("number")
element.click()
Please note that I am waiting till this element becomes actually clickable.
Also, I don't understand why you are using driver1. Use driver for all elements.
Try 2
The answer is easier than I thought. You are messing up with ()
num = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//*[#id='number']")))
Try 3 (more detailed)
wait = WebDriverWait(driver,10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[#id='number']")))
Check here how to use explicit waits https://selenium-python.readthedocs.io/getting-started.html
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
num = WebDriverWait(driver1, 10).until(
EC.visibility_of_element_located((By.XPATH,"//*[#id='number']")))
your webdriverwait syntax is incorrect use the above one , you should not call By.xpath but pass it as a tuple to the EC
also do not use implicit wait ,

Selenium in Python: "NoSuchElementException: Message: no such element: Unable to locate element"

I tried typing 'abc' in the first block of id and 'cdef' in the second block of password.
However, the error code at the bottom comes up.
from selenium import webdriver
driver.get('http://sugang.korea.ac.kr')
I added an implicit wait to prevent the code from executing before the page fully loads.
driver.implicitly_wait(30)
Code for adding username and password is as below:
driver.find_element_by_name('id').send_keys('abc')
driver.find_element_by_name('pw').send_keys('cdef')
But I am getting the below error:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"id"}
The 'No Such Element' exception usually comes when web driver can't see the element you are trying to perform an action on.
Reasons can be:
your ID or Name or XPath or CssSelector can be wrong.
Your element might be inside an an iframe so that web driver can't see or detect it. Switch to an iframe through Selenium and python
Your element is taking time to appear on the UI, so you can use an explicit wait to solve this. See 5. Waits
The username and password fields are within an frame, so 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.
You can use the following solution:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Firefox(executable_path=r'C:\\Utility\\BrowserDrivers\\geckodriver.exe')
driver.get("http://sugang.korea.ac.kr")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"firstF")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.input_login[name='id']"))).send_keys('abc')
driver.find_element_by_css_selector("input.input_login[name='pw']").send_keys("cdef")
Browser Snapshot:
Add explicitly wait
from selenium.webdriver.support import expected_conditions as EC
userNameElement= WebDriverWait(driver, 2).until(
EC.presence_of_element_located((By.NAME, "id"))
userNameElement.send_keys('abc')
pwdElement= WebDriverWait(driver, 2).until(
EC.presence_of_element_located((By.NAME, "pwd"))
pwdElement.send_keys('cdef')
Here, I am expecting that your locators are correct.
It is in a frame which you need to switch to first. Also, use ids where possible as they are faster.
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
url ="http://sugang.korea.ac.kr"
driver = webdriver.Chrome()
driver.get(url)
WebDriverWait(driver,5).until(EC.visibility_of_element_located((By.CSS_SELECTOR,'[name=firstF]')))
driver.switch_to.frame(driver.find_element_by_css_selector('[name=firstF]'))
WebDriverWait(driver,5).until(EC.visibility_of_element_located((By.ID,'id'))).send_keys('abc')
driver.find_element_by_id('pw').send_keys('def')
driver.find_element_by_id('loginButton').click()
The site you are trying to access does not have an element with a tag name id. Examine the site carefully.
<input name="id">
If the input has an id value, try this;
driver.find_element_by_id("id")
Example Use:
HTML:
<div class="form-group">
<input class="form-control" name="username">
</div>
<div class="form-group">
<input class="form-control" name="password" type="password">
</div>
<button id="btn-login" type="submit">Enter</button>
Python:
username = driver.find_element_by_name("username")
password = driver.find_element_by_name("password")
username.send_keys("your_username")
password.send_keys("your_password")
driver.find_element_by_id("btn-login").click()

Finding an element in html page using selenium module in python

I have to find Input Tag for id of a web page in the following HTML code using python Selenium:
NOTE: I was not able to paste the code because of its complexity so sorry for that. Here is the page to look out the complete HTML Tree.
I want to insert username to the mentioned below input tag:
<input type="text" id="appleId" can-field="accountName" autocomplete="off"
autocorrect="off" autocapitalize="off" aria-required="true"
required="required" aria-labelledby="appleIdFieldLabel" spellcheck="false"
autofocus="" ($focus)="appleIdFocusHandler()"
($keyup)="appleIdKeyupHandler()" ($blur)="appleIdBlurHandler()" class="si-
text-field form-textbox " placeholder="Apple ID">
Currently, I am using mentioned below code by the means of element's xpath but it's returning an empty list:
from selenium import webdriver
import time
browser = webdriver.Chrome()
browser.get('http://www.icloud.com')
time.sleep(20)
ele = browser.find_elements_by_xpath('//*[#id="appleId"]')
# ele.send_keys('abc#icloud.com')
print ele
Result: []
I have tried every other find functions too but could not get any results.
Authorization form located inside an iframe. To be able to handle input fields, you should switch to that iframe:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
browser.get('http://www.icloud.com')
wait(browser, 10).until(EC.frame_to_be_available_and_switch_to_it("auth-frame"))
account_name = wait(browser, 10).until(EC.presence_of_element_located((By.ID, "appleId")))
account_name.send_keys('abc#icloud.com')

Categories