如何将文本发送到https://mail.protonmail.com注册页面?

2024-04-25 07:38:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我做了质子邮件脚本当我运行,它是正确的,但它没有输入用户名。此脚本未键入任何文本。请帮帮我!!在

我在倒数第二行有个错误

这是我的错误:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".input"}

我的网络驱动程序有问题

    from selenium import webdriver
    import time

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

    driver = webdriver.Chrome()

    driver.get(url)

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

    time.sleep(10)

    driver.find_element_by_class_name('panel-heading').click()

    time.sleep(10)

    driver.find_element_by_id('freePlan').click()

    time.sleep(10)

    driver.find_element_by_id('username').send_keys(Hamzalachistudios)

    time.sleep(10)

它做得很好,但它没有输入任何文本。错误名称:NoSuchElementException


Tags: 文本import脚本bytimedriver错误sleep
3条回答

您的元素在iframe,所以在click或{}之前的第一步,我们需要switch到{}

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

driver.switch_to.frame("top") //switching the frame by name
driver.find_element_by_id('username').send_keys(Hamzalachistudios)

enter image description here

要将字符序列发送到用户名字段,因为所需元素位于<iframe>内,因此您必须:

  • 诱导WebDriverWait以获得所需的帧,然后切换到该帧。在
  • 诱导WebDriverWait使所需的元素可点击。在
  • 您可以使用以下解决方案:

    • 代码块:

      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
      
      chrome_options = webdriver.ChromeOptions() 
      chrome_options.add_argument("start-maximized")
      # chrome_options.add_argument('disable-infobars')
      driver = webdriver.Chrome(options=chrome_options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
      driver.get("https://protonmail.com/")
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn btn-default btn-short' and @href='signup']"))).click()
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='row']//p[text()='Basic account with limited features']"))).click()
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-primary btn-lg pull-right' and @id='freePlan']"))).click()
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//div[@class='usernameWrap']//iframe[@title='Registration form']")))
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='input' and @id='username']"))).send_keys("Hamza_Mirchi")
      
  • 浏览器快照:

protonmail

Here you can find a relevant discussion on Ways to deal with #document under iframe

您应该使用WebDriverWait

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

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

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.get(url)
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@class="btn btn-default btn-short"]'))).click()
wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'panel-heading'))).click()
wait.until(EC.element_to_be_clickable((By.ID, 'freePlan'))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it("top"))

wait.until(EC.element_to_be_clickable((By.ID, 'username'))).send_keys(Hamzalachistudios)

因为@Amit Jain已经回答你需要切换到iframe,所以我给它添加了等待。。。在

^{pr2}$

相关问题 更多 >