Skip to content
Advertisement

Find Value Using Selenium using a Variable that Contains String

I am trying to open up several URL’s (because they contain data I want to append to a list). I have a logic saying "if amount in icl_dollar_amount_l" then run the rest of the code. However, I want the script to only run the rest of the code on that specific amount in the variable "amount".

Example:

selenium opens up X amount of links and sees ['144,827.95', '5,199,024.87', '130,710.67'] in icl_dollar_amount_l but i want it to skip '144,827.95', '5,199,024.87' and only get the information for '130,710.67' which is in the 'amount' variable already.

Actual results:

Its getting webscaping information for amount '144,827.95' only and not even going to '5,199,024.87', '130,710.67'. I only want it getting webscaping information for '130,710.67' because my amount variable has this as the only amount.

print(icl_dollar_amount_l)

['144,827.95', '5,199,024.87', '130,710.67']

print(amount)

'130,710.67'

file2.py

def scrapeBOAWebsite(url,fcg_subject_l, gp_subject_l):

    from ICL_Awk_Checker import  rps_amount_l2

    icl_dollar_amount_l = []
    amount_ack_missing_l = []
    file_total_l = []
    body_l = []


    for link in url:
        print(link)
        browser = webdriver.Chrome(options=options,
                                   executable_path=r'\TESTuser$TESTDocumentsdriverchromedriver.exe')
        # if 'P2 Cust ID 908554 File' in fcg_subject:
        browser.get(link)
        username = browser.find_element_by_name("dialog:username").get_attribute('value')
        submit = browser.find_element_by_xpath("//*[@id='dialog:continueButton']").click()
        body = browser.find_element_by_xpath("//*[contains(text(), 'Total:')]").text
        body_l.append(body)
        icl_dollar_amount = re.findall('(?:[£$€]{1}[,d]+.?d*)', body)[0].split('$', 1)[1]
        icl_dollar_amount_l.append(icl_dollar_amount)
     if not missing_amount:
        logging.info("List is empty")
        print("List is empty")
     count = 0
     for amount in missing_amount:
        if amount in icl_dollar_amount_l:
            body = body_l[count]
            get_file_total = re.findall('(?:[£$€]{1}[,d]+.?d*)', body)[0].split('$', 1)[1]
            file_total_l.append(get_file_total)

    return icl_dollar_amount_l, file_date_l, company_id_l, client_id_l, customer_name_l, file_name_l, file_total_l, 
           item_count_l, file_status_l, amount_ack_missing_l

Advertisement

Answer

I don’t know if I understand problem but this

 if amount in icl_dollar_amount_l:

doesn’t give information on which position is '130,710.67' in icl_dollar_amount_l and you need also

 count = icl_dollar_amount_l.index(amount)

for amount in missing_amount:
    if amount in icl_dollar_amount_l:
        count = icl_dollar_amount_l.index(amount)
        body = body_l[count]

But it will works if you expect only one amount on list icl_dollar_amount_l. For more elements you would have to use rather for-loop and check every element separatelly

for amount in missing_amount:
    for count, item in enumerate(icl_dollar_amount_l)
        if amount == item :
             body = body_l[count]

But frankly I don’t know why you don’t check it in first loop for link in url: when you have direct access to icl_dollar_amount and body

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement