gpt4 book ai didi

python selenium 'send_keys' 运行不正常

转载 作者:行者123 更新时间:2023-12-02 03:20:54 28 4
gpt4 key购买 nike

我有一个脚本可以打开 eBay 并自动化购买过程,并且它可以工作......一段时间。经过一系列循环后,它随机开始跳过向文本字段发送数据。该脚本第一次总是会完美运行,但在第二次或第十次迭代之间偶尔会崩溃;到目前为止,它还没有超过第 10 个循环。

我使用的是 Python 2.7、Chrome 47(带有 chrome 驱动程序)和 Selenium 2.53.5。

这是我的代码的一部分:

for o in orders:
if (o[1] == "Batteries"):
driver.get("http://www.ebay.com/itm/20-Piece-Combo-Pack-Duracell-Duralock-10-AA-and-10-AAA-Size-Batteries-EXP-2025/272003416650?_trksid=p2045573.c100505.m3226&_trkparms=aid%3D555014%26algo%3DPL.DEFAULT%26ao%3D1%26asc%3D36866%26meid%3D1ff8810857444e0aa548e1aecb205110%26pid%3D100505%26rk%3D1%26rkt%3D1%26")

wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'qtyInput')))
input_field = driver.find_element_by_name('quantity')
input_field.clear()
input_field.send_keys(o[0])
driver.find_element_by_id('binBtn_btn').click()
#GUESTCHECKOUT
wait.until(EC.presence_of_element_located((By.ID, 'gtChk')))
driver.find_element_by_id('gtChk').click()

driver.switch_to_frame(driver.find_element_by_tag_name("iframe"))
wait.until(EC.presence_of_element_located((By.ID, 'confirmEmail')))

t = list(pycountry.countries)
for country in t:
if(o[7] == country.alpha2):
o[7] = country.name

Select(driver.find_element_by_name('country')).select_by_visible_text(o[7])
driver.find_element_by_name('firstName').send_keys(name[0])
driver.find_element_by_name('lastName').send_keys(o[2])

driver.find_element_by_name('address1').send_keys(o[3])

driver.find_element_by_name('city').send_keys(o[4])

Select(driver.find_element_by_name('state')).select_by_value(o[6])
driver.find_element_by_name('zip').send_keys(o[5])

driver.find_element_by_name('dayphone1').send_keys("123")
driver.find_element_by_name('dayphone2').send_keys("456")
driver.find_element_by_name('dayphone3').send_keys("7890")

driver.find_element_by_name('email').send_keys("d@gmail.com")
driver.find_element_by_name('confirmEmail').send_keys("d@gmail.com")

Traceback (most recent call last): File "autoBuyer.py", line 267, in driver.find_element_by_name('lastName').send_keys(name[1]) File "/Library/Python/2.7/site-packages/selenium-2.53.5-
py2.7.egg/selenium/webdriver/remote/webelement.py", line 321, in send_keys self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value':
keys_to_typing(value)}) File "/Library/Python/2.7/site-packages/selenium-2.53.5-
py2.7.egg/selenium/webdriver/remote/webelement.py", line 456, in _execute return self._parent.execute(command, params) File "/Library/Python/2.7/site-packages/selenium-2.53.5- py2.7.egg/selenium/webdriver/remote/webdriver.py", line 236, in execute self.error_handler.check_response(response) File "/Library/Python/2.7/site-packages/selenium-2.53.5- py2.7.egg/selenium/webdriver/remote/errorhandler.py", line 194, 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=51.0.2704.106) (Driver info: chromedriver=2.22.397929
(fb72fb249a903a0b1041ea71eb4c8b3fa0d9be5a),platform=Mac OS X 10.11.5 x86_64)

最佳答案

StaleElementReferenceException 通常指示您与元素交互时刷新的 DOM。我经常在高度动态的网页上看到它们。

如果与 driver.find_element_by_name('dayphone1').send_keys("123") 类似的调用似乎最常引发异常,我将创建一个函数来处理该特定类型的调用,然后用任何 @retry 包对其进行修饰。

我无法运行您编写的代码,但以下是我将进行的更改的演示:(一定要先pip install retry)

from retry import retry
from selenium.common.exceptions import StaleElementReferenceException

@retry(StaleElementReferenceException, tries=3)
def find_by_name_send_keys(driver, name_str, keys, clear=False):
elem = driver.find_element_by_name(name_str)

if clear:
elem.clear()

elem.send_keys(keys)


for o in orders:
if (o[1] == "Batteries"):
driver.get("http://www.ebay.com/itm/20-Piece-Combo-Pack-Duracell-Duralock-10-AA-and-10-AAA-Size-Batteries-EXP-2025/272003416650?_trksid=p2045573.c100505.m3226&_trkparms=aid%3D555014%26algo%3DPL.DEFAULT%26ao%3D1%26asc%3D36866%26meid%3D1ff8810857444e0aa548e1aecb205110%26pid%3D100505%26rk%3D1%26rkt%3D1%26")

wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'qtyInput')))

find_by_name_send_keys(driver, 'quantity', o[0], clear=True)
driver.find_element_by_id('binBtn_btn').click()
#GUESTCHECKOUT
wait.until(EC.presence_of_element_located((By.ID, 'gtChk')))
driver.find_element_by_id('gtChk').click()

driver.switch_to_frame(driver.find_element_by_tag_name("iframe"))
wait.until(EC.presence_of_element_located((By.ID, 'confirmEmail')))

t = list(pycountry.countries)
for country in t:
if(o[7] == country.alpha2):
o[7] = country.name

Select(driver.find_element_by_name('country')).select_by_visible_text(o[7])
find_by_name_send_keys(driver, 'firstName', name[0])
find_by_name_send_keys(driver, 'lastName', o[2])

find_by_name_send_keys(driver, 'address1', o[3])

find_by_name_send_keys(driver, 'city', o[4])

Select(driver.find_element_by_name('state')).select_by_value(o[6])
find_by_name_send_keys(driver, 'zip', o[5])

find_by_name_send_keys(driver, 'dayphone1', "123")
find_by_name_send_keys(driver, 'dayphone2', "456")
find_by_name_send_keys(driver, 'dayphone3', "7890")

find_by_name_send_keys(driver, 'email', "d@gmail.com")
find_by_name_send_keys(driver, 'confirmEmail', "d@gmail.com")

关于python selenium 'send_keys' 运行不正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38132600/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com