Error when playing with python

Instead of memo

I'm going to play with python from now on as a memo I will add it each time

SyntaxError: Non-UTF-8 code starting Error message </ b> SyntaxError: Non-UTF-8 code starting with'Error location' in file sample.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details Cause </ b> The cause was that the python code I was writing was written in S-JIS (SHIFT-JIS) (in short, *** character code ***) Procedure: [New]-> [Text Document]-> Change the extension of the created file from txt to py Punch line </ b> Changed python source code from S-JIS to UTF-8 For terapad: Copy the entire code once → [File] → [Reload with character code] → Select UTF-8 → (After reloading) Select the full text with ctrl + A → Paste with ctrl + V → ctrl + Overwrite with S Even html was garbled, and S-JIS is harmful ... We have to unify it under the UTF-8 god ... (UTF-8 extremism) Faction) </ s> </ font> FileNotFoundError Error message </ b> FileNotFoundError: [Errno 2] No such file or directory: './img.png' Cause </ b> The cause is that the folder (directory) specified by the file does not exist as "img.png " → I mistakenly put img.jpg Punch line </ b> Either the file [img.jpg] should be [img.png] or the img.png in the code should be img.jpg

Part 3 (selenium related)

Error message </ b> selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 81 Cause </ b> The version of chromedriver.exe that I've been using is different from the version of Google chrome on my PC → Chrome Driver 81.0.4044.20 vs. chrome version was 80.0.3987.132 → By the way, how to check the version of chrome can be found on the transition screen of ":" on the upper right → "Help" → "About Google Chrome" Punch line </ b> The version has been chromedriver.exe corresponding to 80.0.3987.132 IndentationError Error message </ b> File "python.py", line 52 ~ IndentationError: expected an indented block Cause </ b> There is no indent (indentation) in the relevant part (52nd line this time) Punch line </ b> Use the Tab key to insert an intent → By the way, half-width space is acceptable (full-width is not allowed) UnicodeDecodeError Error message </ b> UnicodeDecodeError: 'cp932' codec can't decode byte 0x86 in position 49: illegal multibyte sequence Corresponding part (line 49): with open("img.txt") as f: Cause </ b> Apparently it's a character code problem This img.txt is written in UTF-8, but if it is not specified in the Windows environment, it will try to read in S-JIS. Reference: https://qiita.com/Yuu94/items/9ffdfcb2c26d6b33792e S-JIS is harmful ... (UTF-8 extremists) </ s> </ font> Punch line </ b> I was able to rewrite the inside of open as follows Previous) with open ("img.txt") as f: After) with open ("img.txt", encoding = "utf8") as f: SyntaxError: EOL while scanning string literal Error message </ b> print("/、") SyntaxError: EOL while scanning string literal Cause </ b> The cause is that "and" are not closed. In the example sentence, it seems to be closed, but in fact, "" is an escape character </ b> and "" \ "" is a process that "puts" as a character ". Reference: https://blog.pyq.jp/entry/Python_kaiketsu_181122 Punch line </ b> Write continuously like "\"

SyntaxError: Failed to execute 'evaluate' on 'Document' Error message </ b> * @ is a link, so full-width SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//input[@name='name]' is not a valid XPath expression. Applicable part: driver.find_element_by_xpath("//input[@name='name]").send_keys("") Cause </ b> Error that "or' is not closed" Punch line </ b> If you look closely at the corresponding sentence, you will see "" // input [@ name ='name '</ b> </ font>] "" and "'" that should be in the colored part. I didn't have it, so I attached it moved

SyntaxError: (unicode error) Error message </ b>

selenium_test.py


  File "selenium_test.py", line 12
    prefs = {"download.default_directory" : "C:\Users\\{username}\\Downloads\\test"}
                                            ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

Cause </ b> I referred to the source code of the following reference URL, but an error You have to put r that represents the raw data Reference: https://qiita.com/hikoalpha/items/fa8330391823aea2fbca Punch line </ b> Correct </ b>) prefs = {"download.default_directory": r </ b> </ font> "C: \ Users \ {username} \ Downloads \ test "} Wrong) prefs = {"download.default_directory": "C: \ Users \ {username} \ Downloads \ test"} Wrong) prefs = {"download.default_directory": ex (r </ font> "C: \ Users \ {username} \ Downloads \ test" ) </ font>} I'm very grateful for the posting, but the description that seems to induce mistakes if the code that throws an error is left as it is is a bit ... </ font> SyntaxError: invalid character in identifier Error message </ b>

test.py


   while x < xx:
          ^
SyntaxError: invalid character in identifier

Cause </ b> There is a double-byte space </ b> between while and x Punch line </ b> Rewritten to half-width space

element not interactable (selenium related)

Error message </ b>

selenium_test.py


  File "selenium_test.py", line 33, in <module>
    driver.find_element_by_xpath("//button[@class='class']").click()
~ Omitted ~
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

Cause </ b> There were actually multiple buttons with the same element (button with class element "class") It seems that this error will be thrown even if it is off the screen, but in that case you should scroll Reference: https://qiita.com/DNA1980/items/528ff6269986b262acdc Punch line </ b> Use find_elements to spit out all the buttons that match the elements, find the desired button and .click () Bonus </ b> Attribute confirmation code that I often use

selenium_test.py


for tag in driver.find_elements_by_xpath("//button[@class='class']"):#Modify in xpath as needed
#	att = tag.get_attribute("name") #This part is used when you want to pull out a specific part of the caught part → In this case the class is"class"Pull the name element of the button
	print("test:" + tag.text)#If you want to display the above att, click "print("test:" + att)』Becomes

element is not attached to the page document Error message </ b>

selenium_test.py


  File "selenium_test.py", line 33, in <module>
    print("test:"+ele_find.text)
~ Omitted ~
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

Cause </ b> I put a transition process in the for statement, but forgot to describe break For that reason, I think that he was angry at me when he tried to take an element that did not exist although the processing was continuing. Punch line </ b> Screwed break immediately after transition processing

FileNotFoundError: [WinError 3] The specified path cannot be found

Error message </ b>

test.py


os.mkdir("path//test//test2")

Cause </ b> I want to make test2 into test, but the essential test directory did not exist Punch line </ b>

test.py


os.makedirs("path//test//test2") #You can make it all together with makedirs

Characters set with pyautogui are capitalized

Situation </ b>

test.py


pyautogui.typewrite("puyopuyo")#Result → PUYO PUYO

Cause </ b> I was in uppercase input mode with Shift + Caps Lock Probably because it is a command of "hit a key" instead of "enter a key" You will rarely encounter it, Punch line </ b> I was able to go after canceling the capitalization

SyntaxError: invalid syntax Cause </ b> S ... It often appears with syntax errors like excrement ・ I forgot to add ":" at the end of the if statement, for statement, and while statement. Wrong) if a == 0 Correct) if a == 0 : </ b>

・ Some unnecessary characters are included It's usually pointed out, so I'll get rid of it and rewrite it in the correct form.

selenium_test.py


  File "selenium_test.py", line 67
    ActionChains(driver).key_down(Keys.CONTROL).send_keys(,"s").perform()
                                                          ^
SyntaxError: invalid syntax

Recommended Posts