Reputation: 665
I would like to log in the website using selenium. This site has link( members login ) and when I click this link, popup window appears.
The website URL is http://affiliates.888.com/
I wrote the code as follows.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
eight88 = webdriver.Chrome()
eight88.get("http://affiliates.888.com/")
assert "Earn Real Money" in eight88.title
loginForm = eight88.find_element_by_class_name("hide-under-480").click()
# so far popup appears.
eight88.switch_to_alert()
eight88.find_element_by_id("userName").send_keys("username")
eight88.find_element_by_id("password").send_keys("password")
When I run this script, NoSuchElementException
occurs.
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"userName"}
(Session info: chrome=59.0.3071.115)
(Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 4.4.0-83-generic x86_64)
I don't know how to go to the popup and find element there.
How can I log in this website on popup.
Upvotes: 1
Views: 2249
Reputation: 52685
Authorization form is located inside an iframe
, not alert. To be able to handle elements inside iframe
you should switch to it first:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
eight88 = webdriver.Chrome()
eight88.get("http://affiliates.888.com/")
assert "Earn Real Money" in eight88.title
loginForm = eight88.find_element_by_class_name("hide-under-480")
loginForm.click()
wait(eight88, 10).until(EC.frame_to_be_available_and_switch_to_it(eight.find_element_by_xpath('//iframe[contains(@src, "Auth/Login")]')))
eight88.find_element_by_id("userName").send_keys("username")
eight88.find_element_by_id("password").send_keys("password")
Upvotes: 3
Reputation: 864
The pop up is being generated by a JavaScript. So this is a dynamic page. When you use driver.get() you're reading the first static page source that is generated and obviously the username and password fields are not included. First figure out how the JavaScript is invoking the pop up and then use selenium's exec function to execute that JavaScript and another one to authenticate over the result of the first one.
Upvotes: 0