使用Selenium / Python自动化新手。我正在阻止自动注册表单。下拉是必需的元素,但我收到以下错误...
AttributeError:'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'
答案 0 :(得分:2)
选择使用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")
答案 1 :(得分:0)
根据API文档,Select()
接受function showPreview(event){
// ... rest of your code
reader.onload = function(event) {
console.log("this never works : ", event);
console.log(event.target.result);
};
reader.readAsText(file);
}
作为参数,定义如下:
webelement
但是根据您的代码,您已在单引号内传递 teamElement (最初是 WebElement )参数,即class selenium.webdriver.support.select.Select(webelement)
A check is made that the given element is, indeed, a SELECT tag. If it is not, then an UnexpectedTagNameException is thrown.
Args :
webelement - element SELECT element to wrap
。因此,您会看到错误。
将参数 teamElement 作为WebElement传递:
string