因此,我为亚马逊网站编写了一个网站自动化的小脚本,这只是一个测试脚本,我知道由于最低的可能性,这些礼品卡代码都不会有效,但是当我检查很多一段时间后出现回溯错误并停止代码。想知道如何解决
我已经进行了一些研究,并且尝试了一些尝试,但当我使用except时:通过它只是继续执行代码并且不保持检查代码,我希望如果出现错误,它将继续在正确的地方检查代码它离开了,或者帮我解决错误:) 编辑:我还没有看到这个问题是重复的,我为什么要问它,为什么它不同于其他问题?我想知道为什么会发生特定的错误,为什么以及如何使它在发生错误时始终在正确的位置检查代码左,谢谢:)
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
import random
import string
from selenium.webdriver.common.keys import Keys
import os
url = "https://www.amazon.es/gp/buy/payselect/handlers/display.html?hasWorkingJavascript=1"
options = Options()
clear = lambda: os.system('cls')
options.add_argument("user-data-dir=C:\\Users\\PC\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
options.add_argument("--no-sandbox")
clear()
print("Here its the script")
howmany = input("How many codes to test? ")
driver = webdriver.Chrome(options=options, executable_path=r"C:\Users\PC\Desktop\coding\chromedriver")
driver.get(url)
driver.find_element_by_class_name("pmts-apply-claim-code").click()
time.sleep(5)
for i in range(int(howmany)):
randomthreenumber = str(random.randint(0,9))+str(random.randint(0,9))+str(random.randint(0,9))
randomnumber = random.randint(0,9)
randomnumber1 = random.randint(0,9)
randomnumber2 = random.randint(0,9)
randomletter = random.choice(string.ascii_uppercase)
randomletter1 = random.choice(string.ascii_uppercase)
randomthreeletter = random.choice(string.ascii_uppercase)+random.choice(string.ascii_uppercase)+random.choice(string.ascii_uppercase)
randomthreeletter1 = random.choice(string.ascii_uppercase)+random.choice(string.ascii_uppercase)+random.choice(string.ascii_uppercase)
firstpattern = str(randomnumber)+randomthreeletter+randomletter+str(randomnumber1)+randomthreeletter1+str(randomthreenumber)+randomletter1+str(randomnumber2)
driver.find_element_by_class_name("pmts-claim-code").send_keys(firstpattern)
driver.find_element_by_xpath('//*[@name="ppw-claimCodeApplyPressed"]').click()
time.sleep(0.9)
driver.find_element_by_class_name("pmts-claim-code").send_keys(Keys.CONTROL + "a");
driver.find_element_by_class_name("pmts-claim-code").send_keys(Keys.DELETE);
time.sleep(0.5)
checked = "CODE CHECKED"
print (checked)
print("WE ARE DONE CHECKING YOUR CODES!!")
错误:
Traceback (most recent call last):
File "amazonredeemer.py", line 41, in <module>
driver.find_element_by_class_name("pmts-claim-code").send_keys(Keys.DELETE);
File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 479, in send_keys
'value': keys_to_typing(value)})
File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=76.0.3809.100)
期望的结果保持检查代码没有错误吗?