1 pop-up windows dispose and determine whether appears

""" Frame : sometimes,because elements in different iframe ,so unable locate the element, So we need to switch to the iframe use [switch_to_frame] method. as follow:

        browser.switch_to_frame()  //browser.switch_to_frame(browser.find_element_by_id('element'))  :locate iframe with element

        browser.switch_to_window() // switch to specified window

    switch_to_alert()  :Dispose the pop_up window,The pop-up windows have three types,as follow:

        1 (alert) Warning dialog :

        2 (confirm) Confirm message dialog :

        3 (prompt) Prompt message dialog :

        method:

        1 browser.switch_to_alert()  //locate the pop-up windows

        2 browser.switch_to_alert().text() //get test value of pop-up windows

        3 browser.switch_to_alert().accept() // Equivalent to confirmation

        4 browser.switch_to_alert().dismiss() // Equivalent to cancel

        5 browser.switch_to_alert().send_keys() // input value

    switch_to  :

        1 browser.switch_to.default_content() // Returns the main page ,no about frame

        2 browser.switch_to.parent_frame() // Back to the parent frame .Because 'A' and  'B' are samel level framework,So need back to the parent to switch between the two.

        3 browser.switch_to.active_element() // Locates the element currently in focus.

    Get text content of element :

        1 .get_attribute() //For example: browser.find_element_by_id('insurantName3Id').get_attribute('value')

        2 .text  //For example: browser.find_element_by_id('insurantName3Id').text

        3 .js    // context='return document.getElementsByTagName('span')[0].innerText'  /n  browser.execute_script(context)

    The function of [.quit]  : Close the browser,so need to open the browser again.

    Determine whether the popup window appears  : the code as follow

    from selenium.webdriver.support import  expected_conditions as EC

    result=EC.alert_is_present()(browser'driver')

    if result:

        result.accept()

    else:

        sleep(2)

    """

from selenium import webdriver

from time import sleep

from selenium.webdriver.support import  expected_conditions as EC

from selenium.webdriver.common.action_chains import  ActionChains

from selenium.webdriver.support.wait import WebDriverWait

from selenium.webdriver.support.select import Select

browser=webdriver.Chrome()

browser.get('http://10.186.4.171/portal/login/')

browser.implicitly_wait(60)

#browser.set_window_size(400,800)  #  set window_size

Carnumber2 = '050501'

sum = 20

bdh='ANACA08Y1419B000117R'

try:

    browser.maximize_window()

    browser.find_element_by_id("loginAcct").send_keys('liuqin-015')

    browser.find_element_by_id('password').send_keys('Cpic1234')

    browser.find_element_by_xpath('//*[@id="btnLogin"]').click()

    sleep(3)

    # #switch to the frame named'车险理赔查询' to get 'Obtain policy number'

    #browser.switch_to_frame(browser.find_element_by_id("oldIframe")) # use [switch_to_frame] method  switch to next html frame

    #browser.find_element_by_xpath('//*[@id="webmenu"]/ul/li[3]/span/a').click()

    #sleep(2)

    #browser.find_element_by_xpath('//*[@id="webmenu"]/ul/li[3]/div/div[1]/a').click()

    #sleep(2)

    # browser.switch_to_frame(browser.find_element_by_id('bodyIframeId'))  #use [switch_to_frame]  method  switch to next frame  by id

    # browser.switch_to_frame(browser.find_element_by_id('main'))

    # browser.switch_to_frame(browser.find_element_by_id('frame_010502'))  ##use three frames to find element: as follow

    # browser.find_element_by_css_selector('#unitCode').click()

    # sleep(3)

    # browser.find_element_by_xpath('//*[@id="unitCode"]/option[26]').click()

    # sleep(3)

    # browser.find_element_by_xpath('//*[@id="vehicleClaimForm"]/table/tfoot/tr/td[6]/div/input[1]').click()

    # sleep(3)

    # OPNs=browser.find_element_by_css_selector('#C360100VEH19000092-MC360100VEH19000092-ANACA08CTP19B000177Z-2-caseTime2 > td:nth-child(3)').click()

    # # aa=OPNs.get_attribute("cursor: pointer;")

    # # print(aa)

    #switch to '车险报案' to get 'Reprot no'

    browser.switch_to_frame(browser.find_element_by_id('oldIframe'))  #use [switch_to_frame] method switch to next iframe by iframe id

    browser.find_element_by_css_selector('#webmenu > ul > li:nth-child(2) > span > a').click()

    browser.find_element_by_css_selector('#webmenu > ul > li:nth-child(2) > div > div:nth-child(1) > a').click()

    browser.switch_to_frame(browser.find_element_by_id('bodyIframeId'))  # use [switch_to_frame]  method  switch to next frame  by id

    browser.switch_to_frame(browser.find_element_by_id('main'))

    browser.switch_to_frame(browser.find_element_by_id('frame_010301'))  ##use three frames to find element: as follow

    browser.find_element_by_css_selector('#policyNo').send_keys(bdh)

    browser.find_element_by_css_selector('#unitCode').click()

    browser.find_element_by_css_selector('#unitCode > option:nth-child(26)').click()

    browser.find_element_by_xpath('//*[@id="queryClaimForm"]/table/tfoot/tr/td[6]/input[1]').click()

    sleep(2)

    try:

        browser.switch_to_alert().accept()  #accept the pop-up window

    finally:

        sleep(3)

    # browser.switch_to.default_content()    # Returns the main page  with [switch_to.default_content] method

    # browser.switch_to_frame(browser.find_element_by_id('oldIframe'))

    # browser.switch_to_frame(browser.find_element_by_id('bodyIframeId'))  # use [switch_to_frame]  method  switch to next frame  by id

    # browser.switch_to_frame(browser.find_element_by_id('main'))

    # browser.switch_to_frame('frame_010301')

    browser.find_element_by_xpath('//*[@id="0"]/td[1]').click()

    sleep(3)

    browser.find_element_by_xpath('//*[@id="divClaimDetail"]/h5[2]/input[1]').click()

    sleep(3)

    result = EC.alert_is_present()(browser)  #Determine whether the pop_up windows appears

    if result:

        result.accept()

    else:

        sleep(2)

    # try:

    #    browser.switch_to_alert().accept()

    # finally:

    #    sleep(1)

    #Because 'frame_vehicleClaim' and  'frame_010301' are samel level framework,So need back to the parent to switch between the two.

    browser.switch_to.parent_frame()    # Returns the parent frame

    browser.switch_to_frame(browser.find_element_by_id('frame_vehicleClaim'))

    name=browser.find_element_by_id('insurantName3Id').get_attribute('value') # Get text content of element  with [.get_attribute()]  method

    browser.find_element_by_id('notifyMan').send_keys(name)

    browser.find_element_by_css_selector('#driver').send_keys(name)

    sleep(3)

    number=browser.find_element_by_css_selector('#serviceRequestForm > table:nth-child(13) > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(4) > input').get_attribute('value')

    browser.find_element_by_id('telephone').send_keys(number)

    browser.find_element_by_id('driverPhone').send_keys(number)

    sleep(2)

    browser.find_element_by_id('damagePlace').send_keys('22')

    sleep(3)

    browser.find_element_by_id('roadId0').click()

    sleep(5)

    ##

    browser.find_element_by_name('serviceRequest.caseInfo.localClaim').click() # Scene to report

    browser.find_element_by_name('serviceRequest.vehicleDetail.vehicleParas').click()# collision accident

    sleep(2)

    browser.find_element_by_name('serviceRequest.vehicleDetail.vehiclePara').click()# Crashed vehicle

    browser.find_element_by_name('policeProofs').click()

    browser.find_element_by_xpath('//*[@id="tbodyPercentId"]/tr[1]/td[1]/input').click()

    sleep(1)

    Carnumber1 = '江A'

    Carnumber22='江A032700'

    browser.find_element_by_xpath('//*[@id="carObjNo"]').clear()

    browser.find_element_by_xpath('//*[@id="carObjNo"]').send_keys(Carnumber22)

    browser.find_element_by_xpath('//*[@id="divObjectCarid1"]/table[1]/tbody/tr[2]/td[2]/select').click()

    sleep(3)

    browser.find_element_by_xpath('//*[@id="divObjectCarid1"]/table[1]/tbody/tr[2]/td[2]/select/option[2]').click()

    # browser.find_element_by_name('carObjList[0].place').clear()

    # browser.find_element_by_name('carObjList[0].place').send_keys('22')

    # sleep(5)

    # browser.find_element_by_xpath('//*[@id="roadId0"]').click()

    try:

        browser.find_element_by_xpath('//*[@id="divThreeCarid1"]/table[1]/tbody/tr[1]/td/span').text=='三者车信息'

    except :

        browser.find_element_by_css_selector('#serviceRequestForm > table:nth-child(34) > tbody > tr > td > input:nth-child(1)').click()

    if Carnumber2[0] == '0':

        for a in range(sum):

            Carnumber3 = int(Carnumber2) + a

            Carnumber4 = '0' + str(Carnumber3)

            Carnumber = Carnumber1 + Carnumber4

            # print(Carnumber)

            TD = 'cardivThreeCarid1'

            name = "carList[0].lossPart"

            location='#divThreeCarid1 > table.tb_formNew3 > tbody > tr:nth-child(2) > td:nth-child(4) > select > option:nth-child(2)'

            selector = '#divThreeCarid1 > table.tb_formNew3 > tbody > tr:nth-child(4) > td:nth-child(2) > input.btn'

            location1='#divThreeCarid'

            location2=location1 + str(a+1)

            loc=location.replace('#divThreeCarid1',location2)

            sel=selector.replace('#divThreeCarid1',location2)

            TD=TD.replace('1', str(a+1))

            name=name.replace('0',str(a))

            browser.find_element_by_id(TD).clear()

            browser.find_element_by_id(TD).send_keys(Carnumber)

            browser.find_element_by_name(name).click()

            browser.find_element_by_css_selector(loc).click()

            browser.find_element_by_css_selector(sel).click()

            if a< (sum-1):

                browser.find_element_by_css_selector('#serviceRequestForm > table:nth-child(34) > tbody > tr > td > input:nth-child(1)').click()

            else:

                break

        browser.find_element_by_id('imgCbtkId').click()

        browser.find_element_by_name('itemList[0].amount').send_keys('222')

        browser.find_element_by_name('itemList[1].amount').send_keys('222')

        browser.find_element_by_xpath('//*[@id="serviceRequestSubmitBtn"]').click()

        sleep(40)

        try:

            browser.switch_to_alert().accept()

        finally:

            ReportNo=browser.find_element_by_id('showCaseReportNo').text

        print(ReportNo)

        browser.quit()

    else:

        for a in range(sum):

            Carnumber3 = int(Carnumber2) + a

            Carnumber = Carnumber1 + str(Carnumber3)

            # print(Carnumber)

            TD = 'cardivThreeCarid1'

            name = "carList[0].lossPart"

            location = '#divThreeCarid1 > table.tb_formNew3 > tbody > tr:nth-child(2) > td:nth-child(4) > select > option:nth-child(2)'

            selector = '#divThreeCarid1 > table.tb_formNew3 > tbody > tr:nth-child(4) > td:nth-child(2) > input.btn'

            location1 = '#divThreeCarid'

            location2 = location1 + str(a + 1)

            loc = location.replace('#divThreeCarid1', location2)

            sel = selector.replace('#divThreeCarid1', location2)

            TD = TD.replace('1', str(a + 1))

            name = name.replace('0', str(a))

            browser.find_element_by_id(TD).clear()

            browser.find_element_by_id(TD).send_keys(Carnumber)

            browser.find_element_by_name(name).click()

            browser.find_element_by_css_selector(loc).click()

            browser.find_element_by_css_selector(sel).click()

            if a < sum - 1:

                browser.find_element_by_css_selector(

                    '#serviceRequestForm > table:nth-child(34) > tbody > tr > td > input:nth-child(1)').click()

            else:

                break

        browser.find_element_by_id('imgCbtkId').click()

        browser.find_element_by_name('itemList[0].amount').send_keys('222')

        browser.find_element_by_name('itemList[1].amount').send_keys('222')

        browser.find_element_by_xpath('//*[@id="serviceRequestSubmitBtn"]').click()

        sleep(40)

        try:

            browser.switch_to_alert().accept()

        finally:

            ReportNo=browser.find_element_by_id('showCaseReportNo').text

        print(ReportNo)

        browser.quit()

except  BaseException as msg:

    print(msg)

你可能感兴趣的:(1 pop-up windows dispose and determine whether appears)