尝试向“用户名”字段发送文本时找不到元素:[name =“ username”]

时间:2018-09-07 05:24:05

标签: python python-2.7 selenium webdriver webdriverwait

我遇到此错误...

    Error Occurred while sending e-mail!!
('Message: Unable to locate element: [name="username"]\n', 'Error Origin: Proton Mail Script')

当我运行python脚本时...

from selenium import webdriver
from time import sleep
from selenium.webdriver import FirefoxOptions

def send_proton_email(email_to, email_subject, email_message):
    try:
        opts = FirefoxOptions()
        opts.add_argument("--headless")
        driver = webdriver.Firefox(firefox_options=opts)
        driver.get('https://mail.protonmail.com/login')
        driver.find_element_by_id('username').send_keys('emailhere')
        driver.find_element_by_id('password').send_keys('passwordhere')
        driver.find_element_by_id('login_btn').click()
        sleep(3)
        driver.find_element_by_xpath('//*[@id="pm_sidebar"]/section/a').click()
        sleep(2)
        driver.switch_to_active_element().send_keys(email_to + '\n' + '\t' + email_subject + '\t')
        sleep(0.5)
        driver.switch_to_active_element().send_keys(email_message + '\t' + '\t' + '\t' + '\t' + '\t' + '\t')
        sleep(0.5)
        driver.switch_to_active_element().click()
        sleep(5)
        driver.quit()
        print('E-mail Sent!')
        del email_subject
        del email_message
        del driver
    except Exception as err:
        print('\nError Occurred while sending e-mail!!')
        status = (str(err), 'Error Origin: Proton Mail Script')
        print(status)
        del err
        del status
        del driver

receiver=input("Enter your recipients email: ")
subject=input("Enter your subject: ")
msg=input("Enter your message/body: ")

send_proton_email(receiver, subject, msg)

我不明白我得到的错误。元素“用户名”是网站上100%的实际元素。这是一个脚本,可登录到protonmail.com电子邮件,然后将电子邮件发送给您想要的任何人。

这可能是怎么回事?

2 个答案:

答案 0 :(得分:0)

页面源中没有登录表单。它是由JS生成的。您需要等待它出现在DOM中:

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

driver.get('https://mail.protonmail.com/login')

wait(driver, 10).until(EC.visibility_of_element_located((By.ID, "pm_login")))

driver.find_element_by_id('username').send_keys('emailhere')
driver.find_element_by_id('password').send_keys('passwordhere')
driver.find_element_by_id('login_btn').click()

答案 1 :(得分:0)

按照url https://mail.protonmail.com/login作为元素Angular的元素,您必须诱使 WebDriverWait 使所需的元素可点击,并且cn使用以下解决方案:

  • 代码块:

    from selenium import webdriver
    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(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
    driver.get("https://mail.protonmail.com/login")
    WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.margin.loginForm-input-username.ng-pristine.ng-empty.ng-invalid.ng-invalid-required.ng-touched#username"))).send_keys("Frinto")
    driver.find_element_by_css_selector("input.password-input.ng-pristine.ng-untouched.ng-empty.ng-invalid.ng-invalid-required#password").send_keys("Frinto")
    driver.find_element_by_css_selector("button.loginForm-actions-main.pm_button.primary.pull-right.loginForm-btn-submit.disabled-if-network-activity").click()
    
  • 浏览器快照:

protonmail