I created a mafia bot to trawl through a forum thread and collect player voting information and post the information after completion if a command is issued.
Everything is working fine, except that sometimes when I call the post() function, the newlines in my code are not being displayed so everything gets smashed together onto one line.
Oddly enough, when I call the post() function manually, I do not encounter this issue, but about once every 70-100 runs, the bot encounters this problem and spits out this error:
File "C:\Users\Ironstove\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 309, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\Ironstove\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 787, in find_element
'value': value})['value']
File "C:\Users\Ironstove\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in execute
self.error_handler.check_response(response)
File "C:\Users\Ironstove\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="cke_contents_vB_Editor_QR_editor"]/textarea"}
(Session info: chrome=59.0.3071.115)
(Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 6.1.7601 SP1 x86_64)
The thing I find confusing about the error message "Unable to locate element" is that the bot is still posting the message, but missing all of my '\n' characters. The element has clearly been located because the web driver is still sending the keys into the text box, so I am not sure why this error is being raised.
Here is the function in question:
def PostVotes():
print("PostVotes: Posting the votes onto the forum...")
driver.find_element_by_xpath("""//*[@id="cke_contents_vB_Editor_QR_editor"]/textarea""").send_keys("Beep Boop, I'm a bot. Vote count was requested.\n")
driver.find_element_by_xpath("""//*[@id="cke_contents_vB_Editor_QR_editor"]/textarea""").send_keys("\n[b][SIZE=4]Lynch Votes:[/SIZE][/b]\n")
for user in lynchref:
if "PLAYERLIST" not in user and user != "":
driver.find_element_by_xpath("""//*[@id="cke_contents_vB_Editor_QR_editor"]/textarea""").send_keys("[b]",user,"[/b]", " - ", ['{}, '.format(elem) for elem in lynchref[user]], "\n")
driver.find_element_by_xpath("""//*[@id="cke_contents_vB_Editor_QR_editor"]/textarea""").send_keys("\n[b][SIZE=4]Not Voting:[/SIZE][/b]\n")
for player in lynchref:
if player not in lynches and "PLAYERLIST" not in player and player != "":
driver.find_element_by_xpath("""//*[@id="cke_contents_vB_Editor_QR_editor"]/textarea""").send_keys(player,"\n")
driver.find_element_by_xpath("""//*[@id="qr_submit"]""").click()
Here is what the post normally looks like most of the time:
https://puu.sh/wNpbb/8be30d6538.png
And here is what it looks like when the newlines are gone:
https://puu.sh/wNpbJ/98481cd967.png
I just now added a sleep timer to the beginning of the function incase it is due to the website not fully loading, but I don't believe that is the issue (just taking shots in the dark). If anyone has any idea what the problem is, I would really appreciate it!
Edit: After further reading and testing, I discovered that the issue is with Chrome webdriver, and it appears that the element.send_keys tends to have this issue when dealing with large-ish blocks of text > 100 characters. The way I worked around this was to chunk the text and so far things are working fine. I will keep it running and hopefully not see any further issues.