3

I am using Selenium Webdriver with Python for automation testing. While login my application I have got popup message for clicking 'OK' button. where i have used

from selenium import webdriver 
def cbs_login(): 
    browser = webdriver.Firefox() 
    browser.get('172.20.31.1:7085/web/ccp/login') 
    user = browser.find_element_by_css_selector('#userid') 
    user.send_keys('admin') 
    password = browser.find_element_by_css_selector('#password')
    password.send_keys('admin2') 
    login_location = browser.find_element_by_css_selector('#loginlocation') 
    login_location.click() 
    location_name = browser.find_element_by_xpath('/html/body/div[2]/div/div/div‌​/div/div/form/div/di‌​v/div/div/table/tbod‌​y/tr[4]/td/select/op‌​tion[3]')     
    location_name.click() 
    driver.switchTo().alert().accept(); 
    cbs_login()

i can't use the code ,I am getting error like "unresolved reference" Please mention is there any alternate way to handle it by python 2.7 version.

Andersson
  • 51,635
  • 17
  • 77
  • 129
Sarwan
  • 33
  • 1
  • 6
  • what is name of variable of driver – Krishna Satya Aug 23 '17 at 04:50
  • i searched from internet and used this driver command. am not sure about it. – Sarwan Aug 23 '17 at 04:54
  • can you post complete script – Krishna Satya Aug 23 '17 at 04:56
  • from selenium import webdriver def cbs_login(): browser = webdriver.Firefox() browser.get('http://172.20.31.1:7085/web/ccp/login') user = browser.find_element_by_css_selector('#userid') user.send_keys('admin') password = browser.find_element_by_css_selector('#password') – Sarwan Aug 23 '17 at 05:09
  • password.send_keys('admin2') login_location = browser.find_element_by_css_selector('#loginlocation') login_location.click() location_name = browser.find_element_by_xpath('/html/body/div[2]/div/div/div/div/div/form/div/div/div/div/table/tbody/tr[4]/td/select/option[3]') location_name.click() driver.switchTo().alert().accept(); cbs_login() – Sarwan Aug 23 '17 at 05:09
  • i am just a beginner so i might commit lot of mistakes. – Sarwan Aug 23 '17 at 05:13

2 Answers2

2

switchTo() is not a Python method. You should try

browser.switch_to.alert.accept()

Also make sure that you have correctly import WebDriver

Andersson
  • 51,635
  • 17
  • 77
  • 129
0

Please replace driver in as told by @andersson

driver.switch_to.alert.accept()

as browser and try

browser.switch_to.alert.accept()
Krishna Satya
  • 775
  • 2
  • 9
  • 21
  • Thanks :) now i can handle popup window. But now i am facing another problem . while login i have to select my location which will display in drop down. i am providing which drown want to select in the script , it is selecting the location but it is not displaying in location box. please help on this by checking my given code before – Sarwan Aug 23 '17 at 05:21
  • @Sarwan, try to implement code from [this answer](https://stackoverflow.com/questions/7867537/selenium-python-drop-down-menu-option-value/28613320#28613320) – Andersson Aug 23 '17 at 05:46
  • @Andresson, I tried with that scenario, in this i cont able to select the location itself. :( – Sarwan Aug 23 '17 at 10:01
  • from selenium import webdriver from selenium.webdriver.support.ui import Select def cbs_login(): browser = webdriver.Firefox() browser.get('url') user = browser.find_element_by_css_selector('#userid') user.send_keys('admin') password = browser.find_element_by_css_selector('#password') password.send_keys('admin2') select = browser.find_element_by_css_selector('#loginlocation') select.click() select.select_by_visible_text('WAREHOUSE') browser.switch_to.alert.accept(); cbs_login() – Sarwan Aug 23 '17 at 10:20
  • @Sarwan. You implemented it incorrectly. Try `select = Select(browser.find_element_by_id('loginlocation')) select.select_by_visible_text('WAREHOUSE')` – Andersson Aug 24 '17 at 07:51