Validating Log In functionality in website using Selenium webdriver with Python - python

I am trying to click the "Log In" element present in webpage.
"Log In" element is visible , when you click on ACCOUNT element on website.
code is:
import unittest
from selenium import webdriver
class registernewuser(unittest.TestCase):
#classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome()
cls.driver.implicitly_wait(10)
cls.driver.maximize_window()
cls.driver.get("http://demo.magentocommerce.com/")
def test_register_new_user(self):
driver = self.driver
account_click = driver.find_element_by_link_text("ACCOUNT").click()
driver.implicitly_wait(3)
self.driver.find_element_by_link_text('Log In').click()
#classmethod
def tearDownClass(cls):
cls.driver.quit()

Added
[account_click = driver.find_element_by_link_text("ACCOUNT").click() driver.implicitly_wait(3) ]
in code and it worked . If you want to click an element which is a part of dropdown of main element , click main element--> implicitily wait command ---> Click dropdown element.

Related

how can i click this element on the discord page with selenium in python?

Discord Element To Click
I am trying to click the button on the discord login page, i have tried finding the class however the class is random every session i believe and i have tried finding the ID but it does not have an ID.
My goal is to insert text (i've done that already) and click the "Login" button
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(r"C:/Users/sexyv/Downloads/chromedriver_win32/chromedriver.exe")
driver.get("https://discord.com/login")
email_box = driver.find_element(by=By.NAME, value="email")
password_box = driver.find_element(by=By.NAME, value="password")
submit_button = driver.find_element(by=By.ID, value="login-button")
def login(driver, email_box, password_box):
driver.implicitly_wait(3)
driver.implicitly_wait(5)
email_box.send_keys("johnsonkalel15#gmail.com")
password_box.send_keys("Kalel12345shjd")
login(driver=driver,email_box=email_box, password_box=password_box)```
If you want to click on login button use the below locator and click on it.
CSS Selector
button[type='submit']
OR XPATH
//button[#type='submit']

Selenium PhantomJS unable to find elements on page because page is blank

Whenever I run my python selenium test case I get this error:
NoSuchElementException: Message: {"errorMessage":"Unable to find element with name... etc
^^ I can't locate the username field because the page is not loading.
I am able to return the url and it is the correct url.
Whenever I save a screenshot of the login page, it returns a solid white page. PhantomJS is going to the correct address but not loading the page. It looks like this is only happening with https sites and not http.
import unittest
from selenium import webdriver
browser = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true', '-- ssl-protocol=any'])
class TestOne(unittest.TestCase):
def setUp(self):
self.driver = browser
self.driver.set_window_size(2000, 1500)
def test_url(self):
driver = self.driver
self.driver.get("https://urlhere")
print driver.current_url
driver.save_screenshot("path/toscreenshot/screenshot1")
driver.implicitly_wait(30)
driver.find_element_by_name("username").clear()
driver.find_element_by_name("username").send_keys("username")
driver.find_element_by_name("password").clear()
driver.find_element_by_name("password").send_keys("password")
driver.find_element_by_name("submit").click()
# End of login
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()

How to click on confirmation button using Selenium with Python?

I have the following Code that goes to a URL(www.example.com), and clicks on a link(Example 1). (This part works fine)
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://www.example.com")
link = driver.find_element_by_link_text('Example 1')
link.click()
Now, when we click on 'Example 1' link, it opens a confirmation window, with 2 buttons: 'Yes I am authorized user to this site' and 'No I am a new visitor to this site'
So, I wish to click on 'Yes I am authorized user to this site' and then finally enter my log-in credentials.
I have written these 2 lines, just below the above code, for clicking on that button. But these don't work.
button = driver.find_element_by_name("'Yes I am authorized user to this site'")
button.click()
If it is an alert window, you need to use the Alert command.
#import Alert
from selenium.webdriver.common.alert import Alert
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://www.example.com")
link = driver.find_element_by_link_text('Example 1')
link.click()
Alert(driver).accept()
#to dismiss alert
#Alert(driver).dismiss()
I think this would have solved your query.
Based on the comment conversation, I would recommend both using an XPATH search (instead of Name or Id) and waiting for elements to be clickable or loaded. When web-driving or web-scraping, pages may intentionally or accidentally load slowly and this can cause issues if you have pauses or waits either hard coded or non-existent. This snippet of code should allow you to search Google using Selenium and Chromedriver (you can modify the driver function to use Firefox or something else if you'd like):
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
from selenium.common.exceptions import ElementNotVisibleException
from selenium.webdriver.chrome.options import Options
from time import sleep
def init_driver(drvr_path):
chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
driver = webdriver.Chrome(drvr_path+'chromedriver.exe',chrome_options=chrome_options)
driver.wait = WebDriverWait(driver, 5)
return driver
def lookup(query, driver=None, drvr_path=''):
driver = None
if driver is None:
driver = init_driver(drvr_path)
driver.implicitly_wait(45) # Allow up to 45 Seconds for page to load
driver.get("http://www.google.com")
try:
box = driver.wait.until(EC.presence_of_element_located((By.XPATH, """//*[#id="lst-ib"]""")))
box.send_keys(query)
sleep(3) # Let you see the window open
button = driver.wait.until(EC.element_to_be_clickable((By.XPATH,"""//*[#id="sblsbb"]/button""")))
try:
button.click()
except ElementNotVisibleException, s:
print "Error Handled: "+str(s)
button = driver.wait.until(EC.element_to_be_clickable((By.XPATH,"""//*[#id="sblsbb"]/button""")))
try:
button.click()
except:
print "Could not search Google..."
return
resp=driver.page_source.encode('utf-8')
with open(query+'.html','wb') as f:
f.write(resp)
print 'Wrote the File...'
except:
print("Box or Button not found in google.com")
driver.quit()
For example, if your Chromedriver.exe file was located in your default Python path, you could do something like: lookup('Selenium Python XPATH Examples') and it should download an HTML file of the Google Search results. If you already have a Driver initialized, you could of course pass that to it.
Hope this helps
Try this code, hope it will help you
from selenium import webdriver
import time
driver = webdriver.Chrome('path to chromedriver\chromedriver.exe')
driver.get('https://www.example.com')
driver.maximize_window()
link = driver.find_element_by_link_text('Example 1')
link.click()
handles =driver.window_handles # this will give window handles
driver.switch_to.window(handles[1])
button = driver.find_element_by_name("'Yes I am authorized user to this site'")
button.click()

Phantomjs click on checkbox

I'm using selenium and phantomjs and I would like to learn how to click a checkbox properly. For exemple in this page: https://www.udacity.com/courses/android
I would like to check "Free Courses", so I tried this:
from selenium import webdriver
from selenium.webdriver.common.by import By
def __init__(self):
self.driver = webdriver.PhantomJS(executable_path='/usr/local/bin/phantomjs')
def parse(self, response):
self.driver.get(response.url)
element = self.driver.find_element(By.XPATH, '//div[#class="checkbox"]/label[contains(.,"Free Courses")]')
self.driver.execute_script("arguments[0].click();", element)
The problem is that it doesn't seems to be clicking anything: making a screenshot with self.driver.save_screenshot('screenshot.png') it gives all the results, not filtered.
Is it something I'm doing wrong?
Your xpath locates to label element while you want to click on checkbox element, As I'm seeing in your provided website, there is no need to create xpath to select Free Course checkbox, you can simply find this checkbox using By.NAME as below :-
from selenium import webdriver
from selenium.webdriver.common.by import By
def __init__(self):
self.driver = webdriver.PhantomJS(executable_path='/usr/local/bin/phantomjs')
def parse(self, response):
self.driver.get(response.url)
element = self.driver.find_element(By.NAME, 'Free Course')
element.click()
Note :- Selenium provides click() function to perform click on an element, So there is no need to use execute_script to perform click by javascript if you could simply do this by using click() function.
Hope it helps...:)

Select an input element using Selenium

Inspect
Im trying to click on this button to move to the login page.
my code is :
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://moodle.tau.ac.il/')
thats work fine but i can only find the form by using
loginform = driver.find_element_by_xpath("//form[#id='login']/")
I don't know how to get to the button, it's very basic stuff but I didn't find any good example.
This will click on the login button on moodle.tau.ac.il page.
The line driver.find_element_by_xpath(".//*[#id='login']/div/input").click() finds the login button on the page and clicks it. Xpath is just a selector type that you can use with selenium to find web elements on a page. You can also use ID, classname, and CSSselectors.
from selenium import webdriver
driver = new webdriver.Chrome()
driver.get('moodle.tau.ac.il')
# This will take you to the login page.
driver.find_element_by_xpath(".//*[#id='login']/div/input").click()
# Fills out the login page
elem = driver.find_element_by_xpath("html/body/form/table/tbody/tr[2]/td/table/tbody/tr[1]/td[2]/input")
elem.send_keys('Your Username')
elem = driver.find_element_by_xpath("html/body/form/table/tbody/tr[2]/td/table/tbody/tr[3]/td[2]/input")
elem.send_keys('Your ID Number')
elem = driver.find_element_by_xpath("html/body/form/table/tbody/tr[2]/td/table/tbody/tr[1]/td[2]/input")
elem.send_keys('Your Password')
driver.find_element_by_xpath("html/body/form/table/tbody/tr[2]/td/table/tbody/tr[7]/td[2]/input").click()
The page has two identical login forms and your XPath returns the hidden one.
So with the visible one:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get(r"http://moodle.tau.ac.il/")
driver.find_element_by_css_selector("#page-content #login input[type=submit]").click()
Or with an XPath:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get(r"http://moodle.tau.ac.il/")
driver.find_element_by_xpath("id('page-content')//form[#id='login']//input[#type='submit']").click()
You could find it using XPath as mentioned by #ChrisP
You could find it by CSS selector: "#login input[type='text']"
Or you could also just submit the form... loginForm.submit()
Ideally, you'd have a unique id for that button which would make it very easy to find.

Categories