我正在使用pylen 3.5的selenium库制作一个从最高网站购买商品的机器人。机器人可以成功地将项目添加到购物车,但是在结帐过程中,每当它尝试将密钥发送到find_element_by_id()
方法找到的输入元素时,selenium就会抛出错误。这是抛出错误的代码的简化版本。
from selenium import webdriver
d = webdriver.Chrome()
# First it adds an item to the cart
d.get('http://www.supremenewyork.com/shop/tops-sweaters/vxdau6b3t/km1pzdca3')
d.find_element_by_name('commit').click()
# Then it goes to the checkout
d.get('https://www.supremenewyork.com/checkout')
name_box = d.find_element_by_id('order_billing_name')
# This is the line that throws the error
name_box.send_keys('name goes here')
这里是完整的错误消息。
File "error.py", line 7, in <module>
name_box.send_keys('name goes here')
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webelement.py", line 479, in send_keys
'value': keys_to_typing(value)})
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webelement.py", line 628, in _execute
return self._parent.execute(command, params)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.
(Session info: chrome=66.0.3359.181)
(Driver info: chromedriver=2.35.528157 (4429ca2590d6988c0745c24c8858745aaaec01ef),platform=Mac OS X 10.13.3 x86_64)
当我用行name_box = d.find_element_by_id('order_billing_name')
替换行d.find_element_by_xpath("//input[@id='order_billing_name']")
时,代码按预期工作。但是,这不是一个可行的解决方案,因为当以这种方式找到元素时,send_keys()
方法非常慢。谢谢你的帮助!
答案 0 :(得分:1)
让我们尝试正确地做(当然有很多事情要改进,但我试着保持简短。请检查内联评论
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
# First it adds an item to the cart
driver.get('http://www.supremenewyork.com/shop/tops-sweaters/vxdau6b3t/km1pzdca3')
# wait until cart link is available
add_to_cart = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "[name=commit]"))
)
# add to cart
add_to_cart.click()
# wait until checkout link is available
go_to_cart = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "div#cart:not(.hidden) a.checkout"))
)
# Then it goes to the checkout
go_to_cart.click()
# wait until name input is available
name_input = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "div.order_billing_name"))
)
# we can't select pseudo elements with css selector, but we can click on an element
# above it, which is defined in "name_input", and emulate keypresses,
# which are intended for the selected ("click()") element only
actions = ActionChains(driver)
actions.move_to_element(name_input).click().send_keys("hey 123").perform()
driver.quit()
答案 1 :(得分:0)
根据chromedriver change note,请将您的chromedriver升级到2.37或2.38,这与Chrome 66更兼容。
答案 2 :(得分:-1)
此错误消息......
selenium.common.exceptions.WebDriverException: Message: unknown error: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.
...表示当尝试在标识为send_keys()
的元素上调用d.find_element_by_id('order_billing_name')
时,引发了 WebDriverException 。
您可能需要解决以下几个问题:
@ yong的初步分析是正确的,因为二进制版本中似乎存在一些不兼容。
chromedriver=2.35的发行说明明确提到以下内容:
支持 Chrome v62-64
您正在使用 chrome = 66.0
ChromeDriver v2.38的发行说明明确提到以下内容:
支持 Chrome v65-67
根据Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element',frontend enqueue style/script of data is empty or css is empty.时会出现错误。这实质上意味着您使用的Locator Strategy:
d.find_element_by_id('order_billing_name')
hidden
或readonly
。当您在搜索中细化时,您的代码可以正常工作,因为它可以唯一地标识所需的元素,如下所示:
d.find_element_by_xpath("//input[@id='order_billing_name']")
send_keys()
方法非常慢在版本不兼容问题得到解决后一定要小心。