Getting "SyntaxError: unterminated string literal (detected at line 1) (, line 1)" when creating a list by reading the data of promotion from excel file and verifying that list with the Live site promotions data

My code is

*** Settings ***
Library ExcelLibrary
Library OperatingSystem
Library Collections
Library String
Library Process
Library BuiltIn
Library open_folder.py
Library SeleniumLibrary

*** Variables ***
${baseUrl_english} https://shopgivenchybeauty.hk
${baseUrl_chinese} https://shopgivenchybeauty.hk/zh
${screenshot_dir} c:\Screenshot - Copy
${screenshot_dir_ENG} c:\Screenshot\ENG
${screenshot_dir_CN} c:\Screenshot\CN
${annXPath} xpath=//[@id=“section-announcement”]/div/div/div
${annSelectedXPath} xpath=//
[@class=“AnnouncementBar__Content Carousel__Cell is-selected”]
${PromXPath} xpath=//*[@class=“note-additional”]
${EXCEL_FILE} C:\Keval\Dior HK - Promotion.xlsx

*** Test Cases ***
Verify Chinese Announcement
[Documentation] This test case verifies the Chinese announcements and promotions on the website.
${get_list_1} Read Excel ${EXCEL_FILE} Jul 15 2024 24C 25C 26C 27C 28C
Log Many ${get_list_1}
Setup Browser ${baseUrl_chinese}
Capture And Verify Announcements ${get_list_1} ${annSelectedXPath} ${screenshot_dir_CN}
Close All Excel Documents

Verify Chinese Promotions
${get_list_3} Read Excel ${EXCEL_FILE} Jul 15 2024 5C 6C 7C 8C 9C 10C
Log Many ${get_list_3}
Go To Product Page
Verify Promotions ${get_list_3} ${PromXPath} ${screenshot_dir_CN}
Close Browser
Close All Excel Documents

Verify English Announcement
[Documentation] This test case verifies the English announcements and promotions on the website.
${get_list_2} Read Excel ${EXCEL_FILE} Jul 15 2024 24D 25D 26D 27D 28D
FOR ${item} IN @{get_list_2}
Log ${item}
END
Setup Browser ${baseUrl_english}
Capture And Verify Announcements ${get_list_2} ${annSelectedXPath} ${screenshot_dir_ENG}
Close All Excel Documents

Verify English Promotions
${get_list_4} Read Excel ${EXCEL_FILE} Jul 15 2024 5D 6D 7D 8D 9D 10D
FOR ${item} IN @{get_list_4}
Log ${item}
END
Go To Product Page
Verify Promotions ${get_list_4} ${PromXPath} ${screenshot_dir_ENG}
Close Browser
Close All Excel Documents

Open Screenshot Directory
Run Process explorer.exe ${screenshot_dir}

*** Keywords ***
Read Excel
[Arguments] ${filepath} ${sheet} @{rowcolpairs}
Open Excel Document ${filepath} 1
Get Sheet ${sheet}
${get_list} Create List
FOR ${pair} IN @{rowcolpairs}
${row} Set Variable ${pair[:-1]}
${colletter} Set Variable ${pair[-1]}
${colnum} Convert Excel Column Letter To Number ${colletter}
${value} Read Excel Cell ${row} ${colnum}
# Only process and append if the value is not None
Run Keyword If ‘${value}’ != ‘None’ Process And Append Value ${value} ${get_list}
END
Close All Excel Documents
[Return] ${get_list}

Process And Append Value
[Arguments] ${value} ${get_list}
${clean_value} Process Cell Value ${value}
Log ${clean_value}
[Return] ${clean_value}

Process Cell Value
[Arguments] ${value}
${clean_value} Run Keyword If ‘${value}’ != ‘None’ Remove Leading Trailing Whitespace ${value}
… ELSE Set Variable ‘’
# Handle specific characters that may cause issues
${clean_value} Replace String ${clean_value} \n ${EMPTY} # Remove newline characters
# Add additional Replace String steps as needed for other characters
[Return] ${clean_value}

Remove Leading Trailing Whitespace
[Arguments] ${text}
${trimmed_text} Evaluate “${text.strip()}”
[Return] ${trimmed_text}

Convert Excel Column Letter To Number
[Arguments] ${colletter}
${colnum} Evaluate sum((ord(c.upper()) - 64) * 26**i for i, c in enumerate(reversed(list(‘${colletter}’))))
[Return] ${colnum}

Setup Browser
[Arguments] ${url}
Close All Excel Documents # Ensure no Excel documents are open before proceeding
Open Browser ${url} chrome
Set Browser Implicit Wait 10s
Maximize Browser Window
Sleep 5s
Wait Until Element Is Visible //[@id=“onetrust-accept-btn-handler”]
Click Element //
[@id=“onetrust-accept-btn-handler”]
Wait Until Element Is Visible ${annXPath}

Capture And Verify Announcements
[Arguments] ${expected_announcements} ${selected_xpath} ${screenshot_dir}
${extracted_announcements} Create List
${unique_announcements} Create List
${previous_announcement} Set Variable ${EMPTY}
${max_iterations} Set Variable 20 # Increased iterations to handle more cycles
${expected_length} Get Length ${expected_announcements}
FOR ${index} IN RANGE ${max_iterations}
Wait Until Element Is Visible ${selected_xpath}
${text} Get Text ${selected_xpath}
IF ‘${text}’ not in ${unique_announcements}
Append To List ${unique_announcements} ${text}
Append To List ${extracted_announcements} ${text}
# Capture screenshot of the specific section
Capture Element Screenshot ${selected_xpath} ${screenshot_dir}\Announcement_${index}.png
END
${previous_announcement} Set Variable ${text}
# Break the loop if we have captured all expected announcements
${length} Get Length ${extracted_announcements}
Run Keyword If ‘${expected_length}’ == ‘${length}’ Exit For Loop
Sleep 3s
END
Log List ${extracted_announcements}
FOR ${announcement} IN @{expected_announcements}
Should Contain ${extracted_announcements} ${announcement}
END

Go To Product Page
[Arguments] ${url}
Close All Excel Documents # Ensure no Excel documents are open before proceeding
Open Browser ${url} chrome
Set Browser Implicit Wait 10s
Maximize Browser Window
Sleep 5s
Wait Until Element Is Visible //[@id=“section-header”]/div/div[3]/a[2]
Click Element //
[@id=“section-header”]/div/div[3]/a[2]
Sleep 2s
Press Keys none LIPSTICK
Sleep 1s
Press Keys none ENTER
Sleep 1s
Execute JavaScript document.elementFromPoint(950, 740).click()
Sleep 3s
Execute JavaScript window.scrollBy(0, 500)
Wait Until Keyword Succeeds 5s 1s BuiltIn.Sleep 1s # Wait for 1 second

Verify Promotions
[Arguments] ${promotions} ${PromXPath} ${screenshot_dir}
${all_promotions_text} Get Text ${PromXPath}
FOR ${promotion} IN @{promotions}
Should Contain ${all_promotions_text} ${promotion}
END
Capture Element Screenshot ${PromXPath} ${screenshot_dir}\Promotions.png

so here for the test cases “Verify Chinese Promotions” and “Verify English Promotions”.

My Excel File 5C cell content as below:

新客戶登入帳號並於網店首次購物,可獲光彩煥膚防矖乳5ml^

【優惠碼:GIV0624】

I am getting error of

For Chinese promotions:

Starting test: Testing Live.Verify Chinese Promotions
20240620 12:40:48.485 : INFO : ${get_list} =
20240620 12:40:48.486 : INFO : ${row} = 5
20240620 12:40:48.487 : INFO : ${colletter} = C
20240620 12:40:48.488 : INFO : ${colnum} = 3
20240620 12:40:48.488 : INFO : ${colnum} = 3
20240620 12:40:48.489 : INFO :
${value} = 新客戶登入帳號並於網店首次購物,可獲光彩煥膚防矖乳5ml

【優惠碼:GIV0624】
20240620 12:40:48.489 : FAIL :
Evaluating expression ''新客戶登入帳號並於網店首次購物,可獲光彩煥膚防矖乳5ml

【優惠碼:GIV0624】’ != ‘None’’ failed: SyntaxError: unterminated string literal (detected at line 1) (, line 1)
Ending test: Testing Live.Verify Chinese Promotions

For English Promotions

Starting test: Testing Live.Verify English Promotions
20240620 12:35:59.833 : INFO : ${get_list} =
20240620 12:35:59.834 : INFO : ${row} = 5
20240620 12:35:59.835 : INFO : ${colletter} = D
20240620 12:35:59.836 : INFO : ${colnum} = 4
20240620 12:35:59.836 : INFO : ${colnum} = 4
20240620 12:35:59.837 : INFO :
${value} = Receive a Skin Perfecto UV Fluid 5ml^ upon login membership account and complete your first purchase in eShop

[Promo Code: GIV0624]
20240620 12:35:59.838 : FAIL :
Evaluating expression ''Receive a Skin Perfecto UV Fluid 5ml^ upon login membership account and complete your first purchase in eShop

[Promo Code: GIV0624]’ != ‘None’’ failed: SyntaxError: unterminated string literal (detected at line 1) (, line 1)
Ending test: Testing Live.Verify English Promotions

And refer to the screenshot for the live promotion visible on respective website listed in my code itself.

This error is typical when you try to compare a string with newlines in it.
You have to clean-up the string or split by lines and iterate the analysis.

You can remove newlines by using python functions, for example like:

${my_string.replace('\n','')}

or maybe:

${my_string.strip()}

Or using keywords from String library.

@HelioGuilherme66, I already used that in my code, but still getting errors

1 Like

Hi,

This type of comparisons issues can be very struggling to solve, hopefully there always a reason.

Apart from Guilherme hint, check that the strings you’re comparing does not contains non breakable space (nbsp) that might not be human visible and drive a little bit crazy :slightly_smiling_face:

Depending of the comparison/keywords you use, there’s also collapse_spaces option as well as ignore_case

Also the brackets [ / ] inside the variable might be an issue if seen as glob pattern. You should try debug step by step and log obtained strings to verify what’s retrieved from your page.

Regards.
Charlie

1 Like

I tried my best to debug but still getting errors, had also asked Chat GPT but the correction given by Chat GPT is not working.

Search everywhere but, didn’t get the correct solution even I tried Log to debugg but still I am getting error.

Finally I giveup my all inputs and post here.

Can someone can run this my code and help me to debug it.

Regards,

Keval Patel.

Error message says that you are not ending the string.

Both, starting quote and ending quote are different characters (ascii dec 145 and 146). This is the most likely your issue. Use double quotes.

Also worth pointing out is that since we are taking about excel (windows) - line change in windows is actually \n\r not \n and you probably should remove both when removing lines…