Selenium Python - 当元素似乎被隐藏时上传图像
Selenium Python - Upload image when element seems to be hidden
所以基本上我在使用 Selenium 上传一些照片时遇到问题 Python
input 元素似乎隐藏在页面中,所以我 运行 中的 .sendkeys 方法仍然出现一些错误。
这是html输入元素的代码
<div data-react-class="ImageUploadForm" data-react-props="{}" data-react-cache-id="ImageUploadForm-0">
<input class="hidden" type="file" accept="image/jpeg, image/jpg, image/png, image/gif">
<button class="btn btn-lemonfrog text-lg" type="button">Upload photo</button>
</div>
base_path = Path(file).parent
filepath = (basepath / "../core/attachments/clientstackphoto.jpeg").resolve()
hiddenuploaderinput.sendkeys(filepath)
现在在 运行 上面的代码之后我遇到了类型错误:
value = (PosixPath('......./core/attachments/clientstackphoto.jpeg'),)
def keys_to_typing(value):
"""Processes the values that will be typed in the element."""
typing = []
for val in value:
if isinstance(val, Keys):
typing.append(val)
elif isinstance(val, int):
val = str(val)
for i in range(len(val)):
typing.append(val[i])
else:
for i in range(len(val)):
E TypeError: object of type 'PosixPath' has no len()
../../venv/lib/python3.7/site-packages/selenium/webdriver/common/utils.py:150: TypeError
我希望能成功上传照片,也许一些 js 注入会有帮助?
根据您的错误消息,我不完全相信错误消息是由隐藏文件输入引起的。如果是的话,我希望 ElementNotVisibleException
.
但是,我确实看到输入是隐藏的,所以我们应该 运行 一些 JS 来显示输入,也许我们可以将其排除为潜在问题。
显示图像输入的代码
fileInput = driver.find_element_by_xpath("//input[@type='file']")
# display file input so we can send keys
driver.execute_script("arguments[0].style.display = 'block';", fileInput)
或者,您可能需要在 class
属性上执行脚本:
driver.execute_script("arguments[0].setAttribute('class', 'visible')", fileInput)
一旦你执行 JS 使文件输入可见,你就可以像任何其他输入一样 send_keys
它:
fileInput.send_keys("PATH/TO/FILE/HERE")
所以基本上我在使用 Selenium 上传一些照片时遇到问题 Python input 元素似乎隐藏在页面中,所以我 运行 中的 .sendkeys 方法仍然出现一些错误。
这是html输入元素的代码
<div data-react-class="ImageUploadForm" data-react-props="{}" data-react-cache-id="ImageUploadForm-0">
<input class="hidden" type="file" accept="image/jpeg, image/jpg, image/png, image/gif">
<button class="btn btn-lemonfrog text-lg" type="button">Upload photo</button>
</div>
base_path = Path(file).parent
filepath = (basepath / "../core/attachments/clientstackphoto.jpeg").resolve()
hiddenuploaderinput.sendkeys(filepath)
现在在 运行 上面的代码之后我遇到了类型错误: value = (PosixPath('......./core/attachments/clientstackphoto.jpeg'),)
def keys_to_typing(value):
"""Processes the values that will be typed in the element."""
typing = []
for val in value:
if isinstance(val, Keys):
typing.append(val)
elif isinstance(val, int):
val = str(val)
for i in range(len(val)):
typing.append(val[i])
else:
for i in range(len(val)):
E TypeError: object of type 'PosixPath' has no len()
../../venv/lib/python3.7/site-packages/selenium/webdriver/common/utils.py:150: TypeError
我希望能成功上传照片,也许一些 js 注入会有帮助?
根据您的错误消息,我不完全相信错误消息是由隐藏文件输入引起的。如果是的话,我希望 ElementNotVisibleException
.
但是,我确实看到输入是隐藏的,所以我们应该 运行 一些 JS 来显示输入,也许我们可以将其排除为潜在问题。
显示图像输入的代码
fileInput = driver.find_element_by_xpath("//input[@type='file']")
# display file input so we can send keys
driver.execute_script("arguments[0].style.display = 'block';", fileInput)
或者,您可能需要在 class
属性上执行脚本:
driver.execute_script("arguments[0].setAttribute('class', 'visible')", fileInput)
一旦你执行 JS 使文件输入可见,你就可以像任何其他输入一样 send_keys
它:
fileInput.send_keys("PATH/TO/FILE/HERE")