Python: Cannot find object with Selenium - python

Can somebody please help me identify the Login/email, password, and LOG IN elements on this site?
https://lo12poznan.mobidziennik.pl/
I tried using multiple methods but my code couldn't detect them.
I just need the element identification...

I was able to simulate an incorrect login using this script below - if you provide the correct login and password, I think this would work
Script as been written using selenium==4.0.0b4 [ install it using pip install selenium==4.0.0b4]
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
svc=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=svc)
driver.maximize_window()
driver.get("https://lo12poznan.mobidziennik.pl/dziennik/")
time.sleep(5)
driver.find_element(By.ID,'login').send_keys('warsaw#polska.pl')
driver.find_element(By.ID,'haslo').send_keys('MyCryptic#PAASWORD098')
driver.find_element(By.CSS_SELECTOR,"input[type='submit']").click()
driver.quit()

CSS:
Login/e-mail [id="login"]
password [id="haslo"]
submit .btn.btn-block

With XPath style the locators are:
login input field
//input[#id='login']
password input field
//input[#id='haslo']
submit button
//input[#type='submit']
You can use other unique combinations there as well.
And you can use other approaches i.e. by css selector.
Don't forget adding some wait / delay before accessing the elements to let the page loaded before accessing elements there.

you can use find_element_by_id
id for user name is login
id for password is haslo
and for submit button, use find_by_css_selector :-
input[class*='btn-lg']
Remember, xpath usages is not prefer in automation if we happen to find ids. or css.

Related

Unable to find xpath element in a selenium script

I'm trying to use python-selenium script to click on "Sign In" label on a top right corner of the gmail main page.I had used firebug/firepath to find the correct xpath for this class and it seems to be working fine while using browser tools but failed when scripts tries to find same element using xpath. I would greatly appreciate if you can point me to the right direction.Thank you!
Url: https://www.google.com/gmail/about/
PS:I'm relative new to selenium . So please excuse my ignorance if I'm approaching this issue in a wrong manner.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from pyvirtualdisplay import Display
from selenium import webdriver
display = Display(visible=0, size=(1920, 1080))
display.start()
browser = webdriver.Firefox()
browser.get('https://www.gmail.com')
print (browser.title)
g_login=browser.find_element_by_xpath("//a[#class='gmail-nav__nav-link gmail-nav__nav-link__sign-in']")
g_login.click()
You should use the same url you provide above in your post :
browser.get('https://www.google.com/gmail/about/')
Looks like the other redirects to another url and makes the request fail.

Enter data into input field not working with selenium

I'm making basic script for sing-in into Instagram.
I faced with this error
Code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Firefox()
browser.get('https://www.instagram.com/accounts/login/')
username = browser.find_element_by_name("username").send_keys('login')
Try to add
time.sleep(5)
before
username = browser.find_element_by_name("username").send_keys('login')
Might be page not fully loaded
The error reads 'Window not found. The browser window may have been closed'. It happened in line browser.find_element_by_tag_name("body"). The Instagram login page has two such elements, and you need only one of them. You should make the query more specific, for instance by making it return only the first element with the tag.

Logging into website using selenium with python

I am trying to use selenium to log into this website:
but it says the password and login are not visible. I looked around and saw that some people said to wait, but waiting does not seem to help. Here is my code:
# importing libraries
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import re, time, csv
driver = webdriver.Firefox()
driver.get("https://platform.openquake.org/account/login/")
driver.switch_to
driver.maximize_window
time.sleep(10)
username = driver.find_element_by_xpath("//input[#name='username']")
username.send_keys("hi there")
Error message is :
ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Your XPATH actually matches two elements. The non-plural driver methods (find_element_by_XXX) return the first element they find a match for, which in this case is not the one you want.
A good debugging tool for situations like this is to use the plural forms (find_elements_by_XXX) and then see how many elements matched.
In this case, you should do what Tanu suggested and use a more restrictive XPATH:
username = driver.find_element_by_xpath("//div[#class='controls']/input[#id='id_username']")
password = driver.find_element_by_xpath("//div[#class='controls']/input[#id='id_password']")
Modify your xpath:
username = driver.find_element_by_xpath("//div[#class='controls']/input[#id='id_username']")

Handling "Authentication Required" alert box with Python 2.7 + Selenium Webdriver

I am having an issue with a secure URL:
Opening the URL creates an "Authentication Required" alert box with username and password fields.
I am fairly new to Selenium Webdriver and Python. I am not familiar with handling alerts and am currently manually typing in credentials until I can get this figured out. I have already tried adding my username/password into the URL. This does not work for me.
Could someone please point me in the direction of entering keys into username and password fields in an alertbox?
Could you try using Keys to tab within the alert?
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get('http://www.url.com/')
wait(driver, 5).until(EC.alert_is_present())
alert = driver.switch_to_alert()
alert.send_keys('username')
alert.send_keys(Keys.TAB)
alert.send_keys('password')
alert.accept()
In case of such authentication, you need pass username and password to server while accessing page to avoid authentication window(which is out of reach of selenium)
Suppose the url you're trying to access is: http://example.com
you'll have to access this url with credentials like following:
driver.get('http://username:password#example.com')
where username is your username and password is your password for the site.
Thanks for all of the responses. Unfortunately, none of these solutions worked for me. I suspect it may have something to do with the creation of a new profile every time firefox was opened by webdriver.
My workaround:
I changed the driver from Firefox to IE, after installing the 32bit IE driver(http://selenium-release.storage.googleapis.com/index.html?path=2.44/). This solved my issue by no longer creating the alertbox, and allowing me to continue with my unittest.
I was having similar issues where adding my username/password into the URL did not work for me. This was because Firefox was alerting me with a confirmation box requiring me to confirm that I wanted to log into the site with the username provided. The following solved this issue:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('https://<username>:<password>#<site-needing-auth>.com')
alert = driver.switch_to_alert()
alert.accept()
None of the answer before helped with my situation. The website I am authenticating to uses single sign on which was posing issues when using username:password#website.com.
In the Authentication window two fields needed to be entered both User Name and Password.
To solve this send the user name and password at one time to the alert box.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
def login(self):
self.browser = webdriver.Firefox()
self.browser.get(r'websitelogin.com')
wait(self.browser, 1).until(expected_conditions.alert_is_present())
# "Switch" to the Alert browser
alert = Alert(self.browser)
# Send the username, TAB then password all in one go using a python f string
alert.send_keys(f'username{Keys.TAB}password')
alert.accept()
I was having the exact same problems as you until I noticed, that I simply forgot to write: 'https' instead of just http. If you add the 's', for me that did it!
So in code maybe you want to try:
driver.get('https://username:password#domain-name.org')
The below Python code can help to load such a URL prompting for authentication within a JavaScript Popup alert, I was also stuck here for a long time. It's because of Chrome driver will not allow such authentication techniques after the update 59 (probably). There are still backdoors via Selenium using the JavaScript engine in the browser to load such URLs.
driver.get("https://www.google.com")
URL = "https://{username}:{password}#www.example.com"
driver.executeScript("window.open('"+URL+"')")

How to use Selenium with Python?

How do I set up Selenium to work with Python? I just want to write/export scripts in Python, and then run them. Are there any resources for that? I tried googling, but the stuff I found was either referring to an outdated version of Selenium (RC), or an outdated version of Python.
You mean Selenium WebDriver?
Huh....
Prerequisite: Install Python based on your OS
Install with following command
pip install -U selenium
And use this module in your code
from selenium import webdriver
You can also use many of the following as required
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
Here is an updated answer
I would recommend you to run script without IDE... Here is my approach
USE IDE to find xpath of object / element
And use find_element_by_xpath().click()
An example below shows login page automation
#ScriptName : Login.py
#---------------------
from selenium import webdriver
#Following are optional required
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
baseurl = "http://www.mywebsite.com/login.php"
username = "admin"
password = "admin"
xpaths = { 'usernameTxtBox' : "//input[#name='username']",
'passwordTxtBox' : "//input[#name='password']",
'submitButton' : "//input[#name='login']"
}
mydriver = webdriver.Firefox()
mydriver.get(baseurl)
mydriver.maximize_window()
#Clear Username TextBox if already allowed "Remember Me"
mydriver.find_element_by_xpath(xpaths['usernameTxtBox']).clear()
#Write Username in Username TextBox
mydriver.find_element_by_xpath(xpaths['usernameTxtBox']).send_keys(username)
#Clear Password TextBox if already allowed "Remember Me"
mydriver.find_element_by_xpath(xpaths['passwordTxtBox']).clear()
#Write Password in password TextBox
mydriver.find_element_by_xpath(xpaths['passwordTxtBox']).send_keys(password)
#Click Login button
mydriver.find_element_by_xpath(xpaths['submitButton']).click()
There is an another way that you can find xpath of any object -
Install Firebug and Firepath addons in firefox
Open URL in Firefox
Press F12 to open Firepath developer instance
Select Firepath in below browser pane and chose select by "xpath"
Move cursor of the mouse to element on webpage
in the xpath textbox you will get xpath of an object/element.
Copy Paste xpath to the script.
Run script -
python Login.py
You can also use a CSS selector instead of xpath. CSS selectors are slightly faster than xpath in most cases, and are usually preferred over xpath (if there isn't an ID attribute on the elements you're interacting with).
Firepath can also capture the object's locator as a CSS selector if you move your cursor to the object. You'll have to update your code to use the equivalent find by CSS selector method instead -
find_element_by_css_selector(css_selector)
There are a lot of sources for selenium - here is good one for simple use Selenium, and here is a example snippet too Selenium Examples
You can find a lot of good sources to use selenium, it's not too hard to get it set up and start using it.
You just need to get selenium package imported, that you can do from command prompt using the command
pip install selenium
When you have to use it in any IDE just import this package, no other documentation required to be imported
For Eg :
import selenium
print(selenium.__filepath__)
This is just a general command you may use in starting to check the filepath of selenium

Categories