gpt4 book ai didi

python - Selenium w/Python3 - AttributeError : 'str' object has no attribute 'tag_name'

转载 作者:行者123 更新时间:2023-12-01 01:55:30 27 4
gpt4 key购买 nike

Selenium/Python 自动化新手。我无法自动填写注册表单。下拉菜单是必需的元素,但我收到以下错误...

属性错误:“str”对象没有属性“tag_name”

我已经在下面发布了我的代码,但在网上找不到任何关于为什么会这样的答案。非常感谢任何/所有帮助。

from selenium import webdriver
from selenium.webdriver.support.select import Select
teamElement = browser.find_element_by_id('id_team')
time.sleep(2)
sel = Select('teamElement')
sel.select_by_value("12")

错误来自 sel = Select('teamElement') 行。

 Traceback (most recent call last):
File "/Users/jamesstott/PycharmProjects/basics/RunChromeTests.py",
line 40, in <module>
sel = Select('teamElement')
File "/Users/jamesstott/PycharmProjects/venv/lib/python3.6/site-packages/selenium/webdriver/support/select.py", line 36, in __init__
if webelement.tag_name.lower() != "select":
AttributeError: 'str' object has no attribute 'tag_name'

最佳答案

Select 采用 WebElement 类型参数而不是字符串类型。更改以下行

sel = Select('teamElement')

sel = Select(teamElement)

完整代码,

from selenium import webdriver
from selenium.webdriver.support.select import Select
teamElement = browser.find_element_by_id('id_team')
time.sleep(2)
sel = Select(teamElement)
sel.select_by_value("12")

关于python - Selenium w/Python3 - AttributeError : 'str' object has no attribute 'tag_name' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50252138/

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