1
votes

I was making a simple program which enables me to fill out the login form and send email to someone.. and while i'm on it, it just don't go more and stopped at opening some link.

# Practice Program!

import requests, os, bs4, webbrowser
from selenium import webdriver

url = 'http://google.com'

res = requests.get(url)
res.raise_for_status()

browser = webdriver.Firefox()

browser.get(url)

signinElem = browser.find_element_by_id('gb_70')
signinElem.click()

idElem = browser.find_element_by_id('Email')
idElem.send_keys('not_my_real_id')
idElem.submit()

pwElem = browser.find_element_by_id('Passwd')
pwElem.send_keys('not_my_pw')
pwElem.submit()

mailElem = browser.find_element_by_class_name('gb_P')
mailElem.click()

composeElem = browser.find_element_by_class_name('class="T-I J-J5-Ji T-I-KE L3')
composeElem.click()

The code worked well until it hit pwElem, which makes me wonder why signinElem and idElem worked but not pwElem??

I happened to stuck with similar situation, where i can pass through some links, but others not. What is wrong with this code?

And i got following Error

Traceback (most recent call last): File "C:/Users/Cyber/Downloads/WriteMail.py", line 22, in pwElem = browser.find_element_by_id('Passwd') File "C:\Users\Cyber\Downloads\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 269, in find_element_by_id return self.find_element(by=By.ID, value=id_) File "C:\Users\Cyber\Downloads\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 752, in find_element 'value': value})['value'] File "C:\Users\Cyber\Downloads\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute self.error_handler.check_response(response) File "C:\Users\Cyber\Downloads\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"id","selector":"Passwd"} Stacktrace: at (file:///C:/Users/Cyber/AppData/Local/Temp/tmpczr__hhf/extensions/[email protected]/components/driver-component.js:10770) at (file:///C:/Users/Cyber/AppData/Local/Temp/tmpczr__hhf/extensions/[email protected]/components/driver-component.js:10779) at (file:///C:/Users/Cyber/AppData/Local/Temp/tmpczr__hhf/extensions/[email protected]/components/command-processor.js:12661) at (file:///C:/Users/Cyber/AppData/Local/Temp/tmpczr__hhf/extensions/[email protected]/components/command-processor.js:12666) at (file:///C:/Users/Cyber/AppData/Local/Temp/tmpczr__hhf/extensions/[email protected]/components/command-processor.js:12608)

2
show HTML for same\Andersson
+ when trying to click some links, does browser.find_by_id and browser.find_by_class_name make any difference? Is that why some works and others not?Hashnut
@Andersson I tried to parse html code using bs4, but i got []... soup.select('#Passwd'), soup.select('password-shown'), soup.select('input[Passwd]'), all of them.Hashnut
Can you get HTML with your browser? Right click on target element -> Show sourceAndersson
@Andersson Here's the link! docs.google.com/document/d/… I was blocked by some vicious military program so i took some time to make it work..:)Hashnut

2 Answers

1
votes

Paasword field is getting visible when you submit the Email so you need to implement WebDriverWait to wait until password field visible as below :-

from selenium.webdriver.support import expected_conditions as EC

-------------

wait = WebDriverWait(browser, 10)

idElem = wait.until(EC.visibility_of_element_located((By.ID, "Email")))
idElem.send_keys('not_my_real_id')
idElem.submit()


pwElem = wait.until(EC.visibility_of_element_located((By.ID, "Passwd")))
pwElem.send_keys('not_my_pw')
pwElem.submit()

Hope it will help you..:)

0
votes

Actually your error message says:.NoSuchElementException: Message: Unable to locate element

And I see there is a next button on the google login page, right below the Email input box.

So you may click the next button to find the password input box