Trying to automate to buy a shoe from Amazon - python-2.7

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.amazon.com/")
driver.find_element_by_partial_link_text("Sign in").click()
driver.find_element_by_name("email").send_keys("** UR EMAILID **")
driver.find_element_by_name("password").send_keys(" ** UR PASSWORD **")
driver.find_element_by_id("signInSubmit").click()
driver.find_element_by_id("twotabsearchtextbox").send_keys("Mens shoes")
driver.find_element_by_css_selector("#nav-search > form > div.nav-right > div > input").click()
driver.find_element_by_partial_link_text("Fashion Sneakers").click()
dropdown = driver.find_element_by_css_selector("#native_dropdown_selected_size_name")
select = Select(dropdown)
select.select_by_value("4,B01CE7QQPY")
driver.find_element_by_xpath("//*[#id='add-to-cart-button']").click()
I'm able to login, go to mens shoes and select fashion sneakers and select a type of shoe with particular size. Despite selecting its size, I'm not allowed to add to cart. The page says "select the size form the left to add to shopping cart". There are no errors on terminal/command line, but I'm unable to proceed. I have added a screenshot of the screen for reference as image below:
[]

Related

How to identify the Username and Password field to login into USPS.com using Selenium and Python

I am trying to login in USPS.com, my username and password are correct and I am using following code.
user1="myusername"
password1="password"
url="https://reg.usps.com/login?app=MyUSPS"
browser.get(url)
username = browser.find_element_by_name('username')
username.send_keys(user1)
sleep(5)
password = browser.find_element_by_name('password')
password.send_keys(passwd1)
sleep(5)
browser.find_element_by_id('btn-submit').click()
sleep(3)
But when I mannually try to log site show no error.
As per your question and the url https://reg.usps.com/login?app=MyUSPS to login using username and password you need to induce WebDriverWait for the desired elements to be clickable and you can use the following solution:
Code Block:
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
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://reg.usps.com/login?app=MyUSPS")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='form-control' and #id='username']"))).send_keys("Ankit")
driver.find_element_by_xpath("//input[#class='form-control' and #id='password']").send_keys("Ankit")
driver.find_element_by_xpath("//button[#class='btn btn-primary btn-lg btn-block' and #id='btn-submit']").click()
Browser Snapshot:

Cannot find a link

I am trying to click a tab (Regulatory Regional) on a webpage: https://www5.fdic.gov/idasp/advSearchLanding.asp
However, it does not recognize the command. Here, I have attached the code.
import urllib2
import urllib
from bs4 import BeautifulSoup
import subprocess
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
browser = webdriver.Chrome("/usr/local/bin/chromedriver")
import time
s1_url = 'https://www5.fdic.gov/idasp/advSearchLanding.asp'
browser.get(s1_url)
Problem: choose regulatory regional tab but it does not click it.
browser.find_element_by_xpath('//[#id="Banks_Regulatory_Tab"]/a').click()
Got an exception:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="Banks_Regulatory_Tab"]/a"}
Required element located inside an iframe. To be able to handle it you need to switch to that iframe:
browser.switch_to.frame("content")
browser.find_element_by_link_text("Regulatory Regional").click()

unable to locate ImageView element from android in appium (python)

I am trying to find highlighted button in below screenshot of uiautomatorviewer. I am using id of that element, but code gives me NoSuchElementException. I tried using class too but no luck. What's wrong?
uiautomatorviewer screenshot-
code(please check comment for the exact line which gives error)-
import os
from time import sleep
import unittest
import time
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
from appium.webdriver.common.touch_action import TouchAction
# Returns abs path relative to this file and not cwd
PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(__file__), p)
)
class SimpleAndroidTests(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '7.0'
desired_caps['deviceName'] = 'mishra'
desired_caps['app'] = PATH(
'Shopronto.apk'
)
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
#def tearDown(self):
# end the session
#self.driver.quit()
def test_01_correct_username_correct_password(self):
print "test1"
Wait = WebDriverWait(self.driver, 10)
login_tab=Wait.until(EC.presence_of_element_located((By.ID, "com.shopronto.customer:id/tvLogin")))
login_tab.click()
email = self.driver.find_element_by_id("com.shopronto.customer:id/etEmail")
email.send_keys("user1#gmail.com")
password = self.driver.find_element_by_id("com.shopronto.customer:id/etPwd")
password.send_keys("user123")
self.driver.back()
login = self.driver.find_element_by_id("com.shopronto.customer:id/tvBottomBtn")
login.click()
Wait.until(EC.presence_of_element_located((By.ID, "com.android.packageinstaller:id/permission_allow_button"))).click()
#below line gives error
self.driver.find_element_by_id("com.shopronto.customer:id/ivCategory")
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(SimpleAndroidTests)
unittest.TextTestRunner(verbosity=2).run(suite)
When I replace this-
self.driver.find_element_by_id("com.shopronto.customer:id/ivCategory")
with-
mylist=[]
self.driver.back()
mylist = self.driver.find_elements(By.XPATH, "//android.widget.ImageView")
The code keeps running without giving any output (for over 15 mins).
I have also tried this-
Wait.until(EC.presence_of_element_located((By.XPATH, "//android.widget.ImageView[#index='0']"))).click()
But no luck.
I think your all image view's have same ID, So you should use find element by xpath with index as there is no text with image view.

Open link using Selenium on new page

I am clicking the link "Images" on a new page (after searching 'bugs bunny') on Google. It is not retrieving images of the search, rather it is opening the link 'Images' on the old page.
My Code:
import time
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.webdriver.common.keys import Keys
browser = webdriver.Firefox()
browser.get('http://www.google.com')
search = browser.find_element_by_name('q')
search.send_keys("bugs bunny")
search.send_keys(Keys.RETURN) # hit return after you enter search text
browser.current_window_handle
print(browser.current_url)
browser.find_element_by_link_text("Images").click()
Your problem is you are using send_keys, which perform the action and don't wait
search.send_keys(Keys.RETURN) # hit return after you enter search text
So after that if you use click it is doing it nearly on the current page even when the results are not loaded. So you need to add some delay for the return key to change the results and once the results are loaded, you can do the click
So what you need is a simple sleep delay

One Chrome browser window in selenium webdriver --python

I have some issue with the init constructor related to selenium webdriver's chrome instances.
Here are the pieces of my scripts:
First one named methods.py
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import paths
class PagePatterns(object):
def __init__(self, title=None):
self.driver = webdriver.Chrome('C:\Python27\chromedriver.exe')
self.title = title
def get_page_title(self):
return self.get_driver().title
Second one named login.py
from utils.methods import PagePatterns
from selenium.webdriver.common.by import By
from utils import paths
methods = PagePatterns()
class LoginPage(object):
def login(self):
country_choose = (By.XPATH, paths.country_list_login)
methods.click(country_choose)
Next one - register.py
from utils.methods import PagePatterns
from selenium.webdriver.common.by import By
from utils import utils, paths
from selenium.webdriver.support.wait import WebDriverWait
methods = PagePatterns()
class MainPage(object):
def open_homepage(self):
methods.open_base_page()
title_check = methods.get_page_title()
assert title_check == paths.page_title
The last one main_test.py is using LoginPage class
from register import MainPage
from login import LoginPage
MainPage().open_homepage()
LoginPage().login()
That's all about the code. My question is - when I am running main_test.py Chrome is called two times (two windows), but I want only one window of the browser - how to make it?
Thanks.