如何使用Selenium和Python将文本发送到电子邮件字段

时间:2018-07-31 09:31:50

标签: python selenium selenium-webdriver xpath css-selectors

enter image description here

您尝试过类似之前的工作

driver.find_element_by_xpath("//*[@id="loginForm"]/div/div[1]/input").send_keys("abhishek.gupta1608@toppr.in")

但是现在它给出了错误

Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class="inputs"]//input[@name="email"]"}

2 个答案:

答案 0 :(得分:2)

您可以尝试使用名称:

driver.find_element_by_name("email").send_keys("abhishek.gupta1608@toppr.in")  

如果要引入webDriverWait:

wait = WebDriverWait(driver,10)

wait.until(EC.element_to_be_clickable((By.NAME, 'email'))).send_keys("abhishek.gupta1608@toppr.in")  

请注意,您将必须导入以下内容:

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

UPDATE1:

您位于iframe中,您必须将Web驱动程序的焦点切换到默认内容,然后才能与之交互:

driver.switch_to.default_content()

wait.until(EC.element_to_be_clickable((By.NAME, 'email'))).send_keys("abhishek.gupta1608@toppr.in")

答案 1 :(得分:1)

要将字符序列发送到电子邮件字段,您需要诱使 WebDriverWait 使所需的元素可点击,您可以使用任一以下解决方案:

  • CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='email']"))).send_keys("abhishek.gupta1608@toppr.in")
    
  • XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='email']"))).send_keys("abhishek.gupta1608@toppr.in")
    

注意:您必须添加以下导入:

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