[英]Selenium cannot find element by id on supreme website python
I'm making a bot that buys items from the supreme website using the Selenium library for Python 3.5.我正在制作一个使用 Python 3.5 的 Selenium 库从最高网站购买物品的机器人。 The bot can successfully add an item to the cart, but in the checkout process, selenium throws an error whenever it attempts to send keys to an input element found by the
find_element_by_id()
method.机器人可以成功地将商品添加到购物车,但在结帐过程中,只要 selenium 尝试将键发送到由
find_element_by_id()
方法找到的输入元素,就会抛出错误。 Here's a simplified version of the code that throws the error:这是引发错误的代码的简化版本:
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')
Here's the full error message:这是完整的错误消息:
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)
When I replace the line name_box = d.find_element_by_id('order_billing_name')
with the line d.find_element_by_xpath("//input[@id='order_billing_name']")
the code works as intended.当我将
name_box = d.find_element_by_id('order_billing_name')
行d.find_element_by_xpath("//input[@id='order_billing_name']")
,代码按预期工作。 However, this isn't a viable solution as when the element is found this way, the send_keys()
method is very slow.然而,这不是一个可行的解决方案,因为当以这种方式找到元素时,
send_keys()
方法非常慢。
Let's try to do it correctly (of course there are a lot of things to improve, but I tried to keep it short. Please check the comments inline让我们尝试正确地做(当然还有很多地方需要改进,但我尽量保持简短。请检查内联的评论
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()
根据 chromedriver 更改说明,请将您的 chromedriver 升级到 2.37 或 2.38,这与 chrome 66 更兼容。
This error message... 此错误消息...
selenium.common.exceptions.WebDriverException: Message: unknown error: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.
...implies that WebDriverException was raised when the driver was attempted to invoke send_keys()
on the element identified as d.find_element_by_id('order_billing_name')
. ...表示当驱动程序试图在标识为
d.find_element_by_id('order_billing_name')
的元素上调用send_keys()
时引发了WebDriverException 。
There are a couple of things you may need to address as follows : 您可能需要解决以下几项问题:
@yong's initial analysis was in the right direction as there seems to be some incompatibility within the binary versions. @ yong的初步分析是在正确的方向,因为在二进制版本中似乎存在一些不兼容性 。
Release Notes of chromedriver=2.35 clearly mentions the following : chromedriver发行说明= 2.35明确提到以下内容:
Supports Chrome v62-64
支持Chrome v62-64
You are using chrome=66.0 您正在使用chrome = 66.0
Release Notes of ChromeDriver v2.38 clearly mentions the following : ChromeDriver v2.38的发行说明明确提到以下内容:
Supports Chrome v65-67
支持Chrome v65-67
As per Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element' the error is observed when the frontend enqueue style/script of data is empty or css is empty. 根据'在'Window'上执行'getComputedStyle'失败:参数1不是'Element'类型 ,当前端排队样式/数据脚本为空或css为空时,会观察到错误。 .
。 This essentially means the Locator Strategy you have used as :
这实际上意味着您使用的定位策略 :
d.find_element_by_id('order_billing_name')
hidden
or readonly
. hidden
或readonly
其他元素。 When you get granular in your search your code works as it identifies the desired element uniquely as in: 当您在搜索中细化时,您的代码会工作,因为它唯一地标识所需的元素,如:
d.find_element_by_xpath("//input[@id='order_billing_name']")
send_keys()
method is very slow will be surely taken care once version incompatibility issue is taken care. send_keys()
方法很慢,一旦版本不兼容问题得到照顾,一定要小心。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.