1

So I am trying to fill out a form on this site. Every time I try to click the submit button at the end, using what I believe is the correct id, it just gives me an error. Here is a code snippet:

from selenium import webdriver

thePassword = "asdf123"

print("Desired name: ")
name = raw_input()

print("Desired Last Name: ")
userLastName = raw_input()

browser = webdriver.Firefox()
browser.get('https://www.panerabread.com/en-us/mypanera/registration-page.html')

firstName = browser.find_element_by_id('join_first_name')
firstName.send_keys(name)

lastName = browser.find_element_by_id('join_last_name')
lastName.send_keys(userLastName)

emailElem = browser.find_element_by_id('join_email')
emailElem.send_keys("asdafasda" + "@gmail.com")

emailConfirm = browser.find_element_by_id("join_confirm_email")
emailConfirm.send_keys("asdafasda" + "@gmail.com")

password = browser.find_element_by_id("join_password")
password.send_keys("thePassword")

passwordConfirm = browser.find_element_by_id("join_confirm_password")
passwordConfirm.send_keys("thePassword")

phoneA = browser.find_element_by_id("phone_number_a")
phoneA.send_keys("231")

phoneB = browser.find_element_by_id("phone_number_b")
phoneB.send_keys("123")

phoneC = browser.find_element_by_id("phone_number_c")
phoneC.send_keys("2310")

tos = browser.find_element_by_id("join_i_agree")
tos.click()

browser.execute_script("$('#join_password').get(0).scrollIntoView();")
#browser.implicitly_wait(10)

# And then perform the click
browser.find_element_by_id("join_card_not_available").click()

browser.find_elements_by_css_selector("#join-now-primary")[1].click()

print "Your email is: " + "asdafasda" + "@gmail.com"
print "Your password is: " + thePassword

My question is, how can I submit the form at the end of my script?

Edit: There is no error. The problem is that it doesn't click the button I want it to at all. I tried running the below code on a seperate file and it worked, however when you run it with this entire script it does not work.

2
  • What error are you getting? Commented Nov 5, 2015 at 2:05
  • Need the error for sure. Commented Nov 5, 2015 at 10:11

2 Answers 2

1

This was a weird one... it took me a minute to figure out what was going on. The problem is that there are actually two elements that have that id on the page (which is a no-no according to the HTML standard... but alas it happens). One on the bottom of the page that you are looking at and another on the Sign In popup. If you click the Sign In button at the top of the page, you will see the (first) Sign In button on the popup. Because it is hidden, your code wouldn't click on it. Anyway... to the solution.

There are a few ways you can handle this, any of them valid. I would do this.

browser.find_elements_by_css_selector("#join-now-primary")[1].click()

What this is doing is using a CSS selector to get all the elements with ID=join-now-primary. The CSS selector is #join-now-primary which means id (#) of join-now-primary. Because it uses .find_elements (plural), it will get both. We then use [1] to get the 2nd element (0-based index, so 1 is the 2nd) and then click on it.


EDIT

My guess is that it's a timing issue that is causing the code to work on its own but not in your script. Put a breakpoint on the first line and step through each line and make sure that it executes. Does it work? If I were to guess again... it's likely the line right before the Join Now click. That click has an animation that closes the credit card picture. I would wait for that section to become invisible using the code below

element = WebDriverWait(browser, 5).until(EC.invisibility_of_element_located(By.ID('panera-card-section')))
browser.find_elements_by_css_selector("#join-now-primary")[1].click()
Sign up to request clarification or add additional context in comments.

4 Comments

It's not really a no no unfortunately because it happens a lot and browsers allow it. But your answer is right. If you inspect by ID and find multiple elements you need a different locator strategy.
Your code works when I use it in a separate file, however when I run it with all my code it does not work. I edited my question to include the entire code.
Hmm. When you added in the edit, did you make that code in Java? It doesn't seem to work in Python and gives me a syntax error. I edited it to work on Python, but it doesn't seem to work still.
Yep... that was Java sorry. I switch back and forth between Java, C#, and python and forgot that this was a python question. I edited the code and fixed it. The code was not a guaranteed fix, it was an educated guess. You likely have a timing issue. If I get a chance later, I'll look into it some more.
0

You didn't really ask a question but it's likely you need to look at the WebElement class's methods and properties. Looks like the button might not be in the visible portion of the window based on your code. WebElement Has a property call that scrolls until an element moves into view.

If an element is not visible by Selenium definition, it is not clickable.

Even though you use the guts of the page to drive it, selenium wants to pretend it is testing human like interaction and so provides an artificial constraint.

You can bypass that by executing JavaScript click() on the element.

3 Comments

Could you post an example of this with your response? That would be really helpful. I also edited the above question to actually ask a question.
You don't want to use the JSE to click buttons. That's not what an actual user would do.
Well that's basically what Selenium does. It just happens to want you to have it visible to simulate a user. The real question is is that the focus of your test?? Not always. Tests should be narrow in scope. If your test is about inputting data and getting a particular response or even layout on the following page, it's not relevant how you click that button. The correct way would be via Accessibilty scripting using native system events. But we all know the web is a joke for Accessibilty

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.