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
Related
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.
I am trying to use Selenium to sign up an email account automatically whenever I need to. It's just a fun learning project for me. For the life of me I don't understand why it can't find the element. This code works fine on the sign-in page but not the sign-up page. I have tried all different Selenium commands and even tried using the ID and class name. Either is says it can't locate the element or that it is not reachable by keyboard.
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import time
options = Options()
driver = webdriver.Firefox(options=options, executable_path=r'geckodriver.exe')
driver.get("https://mail.protonmail.com/create/new?language=en")
time.sleep(10)
username_input = driver.find_element_by_id("username").send_keys("testusername")
Also here is the HTML code: https://i.imgur.com/ZaBMTzG.png
The username field is in iframe, you need to switch to iframe to make this work.
Below is the code that works fine :
driver.get("https://mail.protonmail.com/create/new?language=en")
driver.switch_to.frame(driver.find_element_by_css_selector("iframe[title='Registration form'][class='top']"))
driver.find_element_by_id("username").send_keys("some string")
read more about iframe here
learn more about how to switch to iframe/frame/framset using Python
selenium Bindings here
Update :
wait = WebDriverWait(driver, 30)
driver.get("https://mail.protonmail.com/create/new?language=en")
driver.switch_to.frame(driver.find_element_by_css_selector("iframe[title='Registration form'][class='top']"))
driver.find_element_by_id("username").send_keys("some string")
driver.switch_to.default_content()
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
sleep(5)
driver.switch_to.frame(driver.find_element_by_css_selector("iframe[title='Registration form'][class='bottom']"))
wait.until(EC.element_to_be_clickable((By.NAME, "submitBtn"))).click()
I'm not sure if I've seen enough code to diagnose, but I think the way you are defining username_input seems problematic. driver.find_element_by_id("username").send_keys("testusername") doesn't actually return anything so it seems like you are setting username_input = null.
I am trying to have Selenium download the URLs of a webpage as PDFs on Safari. So far, I have been able to open the URL, but I can't get Safari to download it. All the solutions I found so far were either for another browser, or they didn't work. Ideally I would like it to download all links of one page and then move on the next page.
At first I thought that clicking on each hyperlink and then downloading it was the way to go. But that would require switching windows each time, so then I tried to find a way to download it without having to click on it, but nothing worked.
I am quite new at programming so I am sure that I am missing something.
import selenium
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
import pdfkit
browser = webdriver.Safari()
browser.get(a_base_url)
username = browser.find_element_by_name("tb_LoginName")
password = browser.find_element_by_name("tb_Password")
submit = browser.find_element_by_id("btn_Login")
username.send_keys(username)
password.send_keys(password)
submit.click()
element=WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//*[#id="maincolumn"]/div/table/tbody/tr[2]/td[9]/a[2]'))).click()
browser.switch_to_window(browser.window_handles[0])
url=browser.current_url
I would go for the following approach:
Get href attribute of the link you want to download via WebElement.get_attribute() function
Use urllib or requests library to retrieve the URL from step 1 without using the browser
Most probably you will also need to get the browser Cookies via WebDriver.get_cookies function and add them to Cookie header for your download request
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.
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']")