1

我对 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:消息:无效元素状态:元素当前不可交互并且可能无法操作

4

1 回答 1

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"]'
于 2017-06-12T10:45:37.860 回答