Reputation: 3082
I am trying to automate packt freebook of each day with selenium in python and cant seem to get to the login form . This is what I have done till now
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
pkt = "https://www.packtpub.com/packt/offers/free-learning"
chromedriver = webdriver.Chrome()
chromedriver.get(pkt)
elem = chromedriver.find_element_by_class_name("twelve-days-claim")
elem.click()
elem2 = chromedriver.find_element_by_class_name("login-popup")
elem2.click()
Till now this thing works but after that I am not able to locate the correct element , what ever I tried results in "element not visible" when I try to send the keys I have tried the following with out any success .
1. username = chromedriver.find_element_by_id("login-form-email")
2.username = chromedriver.find_element_by_id("email")
3.username = chromedriver.find_element_by_id("email-wrapper")
4.username = chromedriver.find_element_by_class_name("cf")
How do I achieve this . I understand that not all elements are visible at start but here after the elem2.click()
this login related buttons should be visible in DOM . Please correct me if I am wrong
Upvotes: 1
Views: 154
Reputation: 871
The only way to avoid an Element not Visible Exception is Explicitly wait in your code till the Element is visible, no matter if you try to find the Element by Xpath, CSS selector or By Id.
Upvotes: 1
Reputation: 763
I tried with CSS selector and it worked for me. Hope it will help you too. Here what I tried:
chromedriver = webdriver.Chrome()
chromedriver.maximize_window()
chromedriver.get(pkt)
elem = chromedriver.find_element_by_class_name("twelve-days-claim")
elem.click()
elem2 = chromedriver.find_element_by_class_name("login-popup")
elem2.click()
username = chromedriver.find_element_by_css_selector("div[id*='form-login'] [id='login-form-email'] input")
username.send_keys( "username" )
Let me know if this solves the issue.
Upvotes: 1