Sebastian Bucarion
Sebastian Bucarion

Reputation: 29

Selenium WebDriver - Unable to locate Element?

Code

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome = webdriver.Chrome(executable_path= 'C:\webdriver.exe\chromedriver.exe',port=9515)
url = 'https://protonmail.com/'
chrome.get(url)
chrome.implicitly_wait(10)
chrome.find_element_by_xpath('//*[@class="btn btn-default btn-short"]').click()
chrome.find_element_by_class_name("panel-heading").click()
chrome.find_element_by_id("freePlan").click()
chrome.find_element_by_id('username')
chrome.find_element_by_id("password").send_keys('password')
chrome.find_element_by_id("passwordc").send_keys('password')

HTML

<input placeholder="Choose username" required="" name="username" messages="[object Object]" iframename="top" pattern=".{1,40}" id="username" class="input">

Problem

chrome.find_element_by_id('username')

I am trying to be able to input a username; however, python says it cannot find the element even though I am using the id it gives you which is username

Upvotes: 0

Views: 72

Answers (2)

NarendraR
NarendraR

Reputation: 7708

There are other things which need to be taken care while you are simulating the action in AUT. i.e. spinner and loader. So you need to handle those as well.

Introduce ExplicitWait in you code. Its been observed that while click on Sign Up button, It starts showing loader and then display the plans So, to handle that use below code until that loader gets hidden and then perform click on required plan.

chrome.find_element_by_xpath('//*[@class="btn btn-default btn-short"]').click()
WebDriverWait(chrome, 20).until(EC.invisibility_of_element_located((By.ID, "redir"))
chrome.find_element_by_css_selector("div[aria-controls='plan-free']").click()

And while perform click on Free Plan button it redirects to new page and show new page loading there and then loads the Signup form. Use below code for that -

WebDriverWait(chrome, 10).until( EC.invisibility_of_element_located((By.ID, "pm_slow"))

And the username in Registration form gets load under iframe so you need to first switch into iframe and then carry further actions

username_frame = chrome.find_element_by_xpath("//div[@class='usernameWrap']//iframe[@title='Registration form']")
chrome.switch_to.frame(username_frame)
WebDriverWait(chrome, 10).until( EC.visibility_of_element_located((By.ID, "username"))
chrome.find_element_by_id('username').send_keys(‘username’)
chrome.switch_to.default_content()
chrome.find_element_by_id("password").send_keys('password')
chrome.find_element_by_id("passwordc").send_keys('password')

Upvotes: 0

Prakhar Jhudele
Prakhar Jhudele

Reputation: 965

Hi I just modified your code and now it works-:

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

import time

chrome = webdriver.Chrome(executable_path= 'C:\webdriver.exe\chromedriver.exe',port=9515)

url = 'https://protonmail.com/'

chrome.get(url)

chrome.implicitly_wait(10)

chrome.find_element_by_xpath('//*[@class="btn btn-default btn-short"]').click()

chrome.find_element_by_class_name("panel-heading").click()

chrome.find_element_by_id("freePlan").click()

time.sleep(10)

#chrome.find_element_by_xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "top", " " ))]')

chrome.switch_to.frame(chrome.find_element_by_xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "top", " " ))]'))
typeinput = chrome.find_element_by_xpath('//*[@id="username"]')
typeinput.click()
typeinput.clear()
typeinput.send_keys('password')

chrome.switch_to.default_content()

chrome.find_element_by_id("password").send_keys('password')

chrome.find_element_by_id("passwordc").send_keys('password')

Upvotes: 1

Related Questions