Finding an element in html page using selenium module in python - 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')

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()

Selenium cannot find element even after giving a high implicitly waiting time

Im trying to automize some webpages and run into the following error:
I have passed the login screen, which gives no problems, but after being redirected to the next page, python gives the following error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:
I have tried to fix it by introducing the driver.implicitly.wait, the time.sleep and the WebDriverWait(driver, time).until(EC.presence_of_element_located((By.ID, "myDynamicElement")) functions, but all the options do not seem to work.
I have shown the relevant HTML code below:
<input type="Text" class="urEdf2TxtEnbl" autocomplete="off" id="DLG_VARIABLE_vsc_CommonVariablesList_VAR_5_VARINPUT_inp" ct="I" name="DLG_VARIABLE_vsc_CommonVariablesList_VAR_5_VARINPUT_inp" st="" tp="STRING" value="502309" onchange="sapUrMapi_InputField_change('DLG_VARIABLE_vsc_CommonVariablesList_VAR_5_VARINPUT_inp',event);" onblur="sapUrMapi_InputField_Blur('DLG_VARIABLE_vsc_CommonVariablesList_VAR_5_VARINPUT_inp',event);" onkeydown="sapUrMapi_InputField_keydown('DLG_VARIABLE_vsc_CommonVariablesList_VAR_5_VARINPUT_inp',event);" onkeyup="sapUrMapi_InputField_KeyUp('DLG_VARIABLE_vsc_CommonVariablesList_VAR_5_VARINPUT_inp',event);" onfocus="sapUrMapi_InputField_focus('DLG_VARIABLE_vsc_CommonVariablesList_VAR_5_VARINPUT_inp',event);" onselectstart="sapUrMapi_InputField_onselectstart('DLG_VARIABLE_vsc_CommonVariablesList_VAR_5_VARINPUT_inp',event);">
The HTML code in the region around this is shown below:
<input type="Text" class="urEdf2TxtEnbl" autocomplete="off" id="DLG_VARIABLE_vsc_CommonVariablesList_VAR_4_VARINPUT_inp" ct="I" name="DLG_VARIABLE_vsc_CommonVariablesList_VAR_4_VARINPUT_inp" st="" tp="STRING" value="" onchange="sapUrMapi_InputField_change('DLG_VARIABLE_vsc_CommonVariablesList_VAR_4_VARINPUT_inp',event);" onblur="sapUrMapi_InputField_Blur('DLG_VARIABLE_vsc_CommonVariablesList_VAR_4_VARINPUT_inp',event);" onkeydown="sapUrMapi_InputField_keydown('DLG_VARIABLE_vsc_CommonVariablesList_VAR_4_VARINPUT_inp',event);" onkeyup="sapUrMapi_InputField_KeyUp('DLG_VARIABLE_vsc_CommonVariablesList_VAR_4_VARINPUT_inp',event);" onfocus="sapUrMapi_InputField_focus('DLG_VARIABLE_vsc_CommonVariablesList_VAR_4_VARINPUT_inp',event);" onselectstart="sapUrMapi_InputField_onselectstart('DLG_VARIABLE_vsc_CommonVariablesList_VAR_4_VARINPUT_inp',event);">
What I am trying to do is to change the value: value="502309". See below for the full code:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
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
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('xxxx')
login = driver.find_element_by_xpath('xxx')
login.send_keys('xxx')
passw = driver.find_element_by_xpath('xxx')
passw.send_keys('xxx')
button = driver.find_element_by_xpath('xxx')
button.click()
driver.maximize_window()
driver.implicitly_wait(120)
mat = driver.find_element_by_id('DLG_VARIABLE_vsc_CommonVariablesList_VAR_2_VARINPUT_inp')
driver.close()
Try to print the html using beautifulsoup so you can check if the element really exist. For your reference on how to use it, see link
I don't know if this will help but i had the same problem and solved it by this:
searchbox = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located(
(By.XPATH, "your_xpath_here")
The above code shows that the webdriver will wait's for an element located in your page.
Issue with your code is not wait , but rather correct element locator. As far as I can see from limited HTML DOM you have provided, part of name and id of element you want to locate inside variable mat is changing. You can locate your element using below code ( Also i have included more sophisticated way to wait for element load).
mat = WebDriverWait(driver, 120).until(EC.presence_of_element_located((By.XPATH, "//input[starts-with(#id,'DLG_VARIABLE_vsc_CommonVariablesList_VAR')]")))

How can I send keys to a hidden password box

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

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()

Selenium python on instagram

I'm trying to do some web-scraping on instagram with selenium. specifically i'm trying to log-in by this address
https://www.instagram.com/accounts/login/
with selenium.
On this page, the input 'username' is written like this
<input class="_ph6vk _o716c" aria-describedby="" aria-label="Phone number, username, or email" aria-required="true" autocapitalize="off" autocorrect="off" maxlength="30" name="username" placeholder="Phone number, username, or email" value="" type="text">
What i'm doing in python is:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
driver=webdriver.Firefox()
driver.get('https://www.instagram.com/accounts/login/')
input_username = driver.find_elements_by_xpath("//input[#name='username']")
input_username.send_keys("username")
Python returns me : AttributeError: 'list' object has no attribute 'send_keys'
So I did the same changing this:
input_username[0].send_keys("username")
And the error is:
IndexError: list index out of range
So, the array is empty. Anyone knows how to solve it?
In your case the page might not have loaded the form, so use WebDriverWait to let the element load and start scraping.
You can check for the element this way, instead of putting time.sleep(2) because it might take a long time to load it as well.
myElem = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))
Also try to use the api if it's possible, scraping should be the second approach.
Try this code:
from selenium import webdriver
import time
driver=webdriver.Firefox()
driver.get('https://www.instagram.com/accounts/login/')
time.sleep(2)
user_name=driver.find_element_by_xpath('//*[#id="react-root"]/section/main/div/article/div/div[1]/div/form/div[1]/div/input')
user_name.send_keys('user_name')
password=driver.find_element_by_xpath('//*[#id="react-root"]/section/main/div/article/div/div[1]/div/form/div[2]/div/input')
password.send_keys('pa$$')
driver.find_element_by_xpath('//*[#id="react-root"]/section/main/div/article/div/div[1]/div/form/span[1]/button').click()
The form loads after the page is loaded so here's how I did it :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Firefox()
driver.get("https://instagram.com")
time.sleep(4)
driver.find_element_by_name("username").send_keys("foobar")
driver.find_element_by_name("password").send_keys("pass")
driver.find_element_by_name("password").send_keys(Keys.ENTER)
A bit hacky at the end

Categories