0

I have the following HTML

<button name="_eventId_confirmed" class="btn btn-green margin-bottom-20 bold medium" autofocus="">

and the following Python

btn = driver.find_element_by_name('_eventId_confirmed')

Running this code returns an error

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [name="_eventId_confirmed"]

Just before this non-working HTML/code-combination I have the following HTML element:

<input name="registration" id="registration" class="size-28" maxlength="7" autofocus="" autocomplete="off" type="text" data-original-title="" title="" style="">

which I successfully access with

elem = driver.find_element_by_name("registration")

Why does the second one work but not the first one?

Edit: the problem was that a new window was opened and I needed to switch window handler. So, false alarm. Thank you all.

3 Answers 3

1

A "tag name" is not the same as a "name". The former refers to the HTML element's tag name, while the later refers to the HTML element's "name" attribute.

For example, in your first HTML snippet,

<button name="_eventId_confirmed" class="btn btn-green margin-bottom-20 bold medium" autofocus="">

button is the tag name while _eventId_confirmed is the (attribute) name.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, the I posted incorrect code. I just tried with tag name after failing using just name. I'll update the post.
@d-b I see. In that case, I don't see any obvious reason why it's failing--but there could be a couple. When I use Python, I like to debug issues like this using the Python interpreter to open up the webpage and calling the Selenium commands on it manually. If it works, then the problem is usually a timing/loading issue; if it doesn't, then it's usually some weird iframe or "responsive website"-related thing
I was starting to reason along those lines too. This is just the second step in a flow. First I enter some data, clicks OK. This causes a new window to open and a modal dialogue is displayed. It is a button in this modal dialogue I want to access. Can it be that I somehow lose the session due to this new window? I even tried a stupid xpath copied directly from Firefox /html/body/div[1]/form/div[3]/div/div/div[2]/div[3]/button but even then the element was missing.
1

This could be because of the modal dialogue, as you mentioned in a comment. Try

driver.switchTo().frame("ModalFrameTitle");

or

driver.switchTo().activeElement()

Comments

1

You can do it by using window_handles and switch_to_window method.

Before clicking the button the window handle as

window_before = driver.window_handles[0]

elem = driver.find_element_by_name("registration")

after clicking the button the window handle of newly opened window as

window_after = driver.window_handles[1]

then execute the switch to window methow to move to newly opened window

driver.switch_to.window(window_after)

driver.find_element_by_name("_eventId_confirmed").click()

Hope this help.

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.