0

I just want to write a simple log in code for one website. However, I think the log in page was written in JS. It's really hard to locate the elements with selenium. The web page I am going to play with is: "https://www.nike.com/snkrs/login?returnUrl=%2F"

This is how the page looks like and how the inspect element page look like:

I was trying to locate the element by following code:

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys
import time 
driver = webdriver.Firefox()
driver.get("https://www.nike.com/snkrs/login?returnUrl=%2Fthread%2Fe9680e08e7e3cd76b8832684037a58a369cad5ed")
time.sleep(5)
driver.switch_to.frame(driver.find_element_by_tag_name("iframe")) 
elem =driver.find_element_by_xpath("//*[@id='ce3feab5-6156-441a-970e-23544473a623']") 
elem.send_keys("pycon") 
elem.send_keys(Keys.RETURN) 
driver.close()

This code return the error that the element could not find by [@id='ce3feab5-6156-441a-970e-23544473a623'.

I tried playing with frames, it seems does not work. If I went to "view web source" page, it is full of JS code.

Is there a good way to play with such a web page with selenium?

3
  • I am sorry that my code is not well formatted. Commented Jun 2, 2016 at 19:18
  • 1
    If you know that your code is not well formatted, why didn't you fix it? ;) Commented Jun 2, 2016 at 19:59
  • WOWOWOWOW, how did you do that, i tried, and i thought I could not modify it after I post it....I need to learn how to use this website. Thanks for your help. Commented Jun 4, 2016 at 1:33

2 Answers 2

0

Try changing the code :

elem =driver.find_element_by_xpath("//*[@id='ce3feab5-6156-441a-970e-23544473a623']") 

to

elem =driver.find_element_by_xpath("//*[@type='email']") 
Sign up to request clarification or add additional context in comments.

4 Comments

It works!! great thanks!! I am a new bird here, so happy to get answer to a problem that confused me for a week...
Hi Bud, I met a new problem. With the same link above, click "join now" will enter a sign up page. There is one element named "date of birth". Actually, i am able to locate the element, however, i cannot use element.send_keys("12081922") to fill that line. There was not error with the code, just failed to fill the form correctly. I also tried element.click() first, then element.send_keys("12081922"), no error, but failed to fill the form.
@Luke - could you please initiate that in a different thread, would be better to understand. Seems like its not dependent on this anyhow.
Sure, may I trouble you to look at my another problem: stackoverflow.com/questions/37705631/…
0

My guess (and observation) is that the id changes each time you visit the page. The id looks auto-generated, and when I go to the page multiple times, the id is different each time.

You'll need to search for something that doesn't change. For example, you can search for the name attribute, which has the seemingly static value "emailAddress"

element = driver.find_element_by_name("emailAddress")

You could also use an xpath expression to search for other attributes, such as data-componentname:

element = driver.find_element_by_xpath("//input[@data-componentname='emailAddress']")

Also, instead of a hard-coded sleep, you can simply wait for the element to be visible:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get("https://www.nike.com/snkrs/login")

element = WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.NAME, "emailAddress"))
)

1 Comment

Hi Bryan, I met a new problem. With the same link above, click "join now" will enter a sign up page. There is one element named "date of birth". Actually, i am able to locate the element, however, i cannot use element.send_keys("12081922") to fill that line. There was not error with the code, just failed to fill the form correctly. I also tried element.click() first, then element.send_keys("12081922"), no error, but failed to fill the form.

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.