Python& Selenium:元素目前无法互动,可能无法操纵

时间:2017-06-12 10:36:41

标签: python selenium quora

我是Selenium的新手,所以在尝试登录 Quora 网站之后:

# gets email and password from json
    with open('config.json') as f:
        login_data = json.load(f)
    email = login_data['email']
    password = login_data['pass']

    email_field_xpath = "//input[@name='email']"
    password_field_xpath = "//input[@name='password']"
    login_button_xpath = "//input[@value='Login']"

    # webdriver's going to wait max 10 seconds for email's field, password field, login button to display
    email_field_element = WebDriverWait(driver, 10).until(
        lambda driver: driver.find_element_by_xpath(email_field_xpath))
    password_field_element = WebDriverWait(driver, 10).until(
        lambda driver: driver.find_element_by_xpath(password_field_xpath))

    email_field_element.clear()
    email_field_element.send_keys(email)
    password_field_element.clear()
    password_field_element.send_keys(password)

    # wait till element is visible before interaction
    login_button_element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, login_button_xpath)))
    login_button_element.click()

我发现了这个错误:

  

追踪(最近一次通话):   ...

     

文件" quora.py",第39行,在log_in中       email_field_element.clear()

     

文件" /home/annieshlepak/work/QuoraAutomatization/env/lib/python3.5/site-packages/selenium/webdriver/remote/webelement.py" ;,第92行,清除       self._execute(Command.CLEAR_ELEMENT)   ...

     

selenium.common.exceptions.InvalidElementStateException:消息:无效的元素状态:元素当前不可交互,可能无法操作

1 个答案:

答案 0 :(得分:1)

有两个input元素具有name="email"属性(您可以使用len(driver.find_elements_by_xpath(email_field_xpath))进行检查)

尝试按索引

找到所需(第二个)元素
driver.find_elements_by_xpath(email_field_xpath)[1].clear()

或使用更具体的XPath

email_field_xpath = '//div[@class="form_column"]/input[@name="email"]'