2

Newbie to automation with Selenium/Python. I'm getting blocked automating a sign up form. The drop down is a required element but I'm getting the following error...

AttributeError: 'str' object has no attribute 'tag_name'

I've posted my code below and can't find any answer online as to why this would be. Any/all help greatly appreciated.

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")

The error is coming from the sel = Select('teamElement') line.

 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'
3
  • can you post your entire example? Commented May 9, 2018 at 11:30
  • which line you are getting the error? Commented May 9, 2018 at 11:31
  • Sorry, yes I should say that the error is coming from the sel = Select('teamElement') line. 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' Commented May 9, 2018 at 11:33

2 Answers 2

4

Select takes WebElement type argument not string type. change the following line

sel = Select('teamElement')

to

sel = Select(teamElement)

Full code,

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")
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, thank you. That worked. I can't believe it was that simple.
0

As per the API Docs, Select() accepts webelement as an argument and is defined as follows :

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

But as per your code you have passed the argument teamElement (which was originally a WebElement) within single quotes i.e. as a string. Hence you see the error.

Solution

Pass the argument teamElement as a WebElement as :

sel = Select(teamElement)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.