【问题标题】:Selenium AttributeError 'list' object has no attribute send_keysSelenium AttributeError 'list' 对象没有属性 send_keys
【发布时间】:2021-06-05 22:35:07
【问题描述】:

我想使用 Selenium 自动登录网站 https://clientes.ecuabots.com/user/login

这是我的代码:

class EcuabotsTest(unittest.TestCase):

def setUp(self):
    self.driver =  webdriver.Chrome(executable_path='/mnt/c/Users/Barbara/Documents/Formación Continua/Selenium/chromedriver.exe')
    driver = self.driver
    driver.get('https://clientes.ecuabots.com/user/login')
    driver.maximize_window()
    #driver.implicitly_wait(15)

def test_search_email_input(self):
    email_field = self.driver.find_elements_by_xpath('//*[@id="input-15"]')
    email_field.clear()
    
    email_field.send_keys('email@email.com)



if __name__ == "__main__":
    unittest.main(verbosity=2)

但是当我尝试这种方式时出现错误:AttributeError 'list' object has no attribute send_keys

  • 我尝试使用 email_field = self.driver.find_element_by_xpath('//[@id="input-15"]') (单数)但我收到此错误:selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:{"method":"xpath","selector":"//[@id="input-15"]"

  • 我尝试使用 id、CSS 选择器和完整 XPath 查找电子邮件输入,但它不起作用

  • 我尝试使用 email_field[0].send_keys('email@email.com) 但我再次收到第一个错误

非常感谢您的帮助!

【问题讨论】:

    标签: python python-3.x selenium selenium-webdriver selenium-chromedriver


    【解决方案1】:
    1. 您必须等到页面加载完毕并在那里显示元素
    2. 你应该使用find_element_by_xpath,而不是find_elements_by_xpath
    3. 您的定位器错误

    试试这个:

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    email_input_css = 'input[placeholder="Email Address"]'
    
    wait = WebDriverWait(browser, 20)
    
    email = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, email_input_css)))
    email_field.clear()
    email_field.send_keys('email@email.com)
    
    

    【讨论】:

    • 您可以使用任何元素属性值。
    猜你喜欢
    • 2021-04-06
    • 2019-11-22
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-28
    • 1970-01-01
    相关资源
    最近更新 更多