Selenium send_keys说:元素当前不可交互,可能无法操作

时间:2017-08-20 07:28:37

标签: python selenium selenium-webdriver phantomjs

我想将密钥发送到登录表单。问题是错误说:

Element is not currently interactable and may not be manipulated

这是我的代码:

url = 'http://tx3.travian.ir'
hero_url = 'http://tx3.travian.ir/spieler.php?uid=19865'
driver = webdriver.PhantomJS(r'phantomjs/bin/phantomjs')
try:
    driver.set_page_load_timeout(15)
    driver.get(url)
except:
    Debug.PrintException()
wait = WebDriverWait(driver, 10)
username = wait.until(EC.visibility_of_element_located((By.NAME, 'name')))
password = wait.until(EC.visibility_of_element_located((By.NAME, 'password')))
username.send_keys('user')
password.send_keys('pass')
login_btn = driver.find_element_by_id('s1')
login_btn.click()
driver.get(hero_url)
driver.close()

出了什么问题?

3 个答案:

答案 0 :(得分:0)

您定义了WebDriverWait,但您从未使用过它

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions

wait = WebDriverWait(driver, 10)
username = wait.until(expected_conditions.visibility_of_element_located((By.NAME, 'name')))

答案 1 :(得分:0)

在输入框中尝试click,然后输入文字。

有时,java中的thread.sleep(ms)也有帮助。

答案 2 :(得分:0)

我使用了您自己的代码并进行了5次调整,如下所示:

  1. window_size设置为(1400,1000)
  2. 对于username字段而不是(By.NAME, 'name'),我使用了(By.XPATH, "//input[@name='name']")
  3. 对于password字段而不是(By.NAME, 'password'),我使用了(By.XPATH, "//input[@name='password']")
  4. 对于login_btn按钮而不是find_element_by_id('s1'),我使用了find_element_by_xpath("//button[@id='s1']")
  5. 最后,我添加了一个print语句来确认脚本结束。
  6. 以下是工作代码块:

    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 = 'http://tx3.travian.ir'
    hero_url = 'http://tx3.travian.ir/spieler.php?uid=19865'
    driver = webdriver.PhantomJS(executable_path=r'C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe')
    try:
        driver.set_page_load_timeout(15)
        driver.set_window_size(1400,1000)
        driver.get(url)
    except:
    #     Debug.PrintException()
        pass
    wait = WebDriverWait(driver, 10)
    username = wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@name='name']")))
    password = wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@name='password']")))
    username.send_keys('user')
    password.send_keys('pass')
    login_btn = driver.find_element_by_xpath("//button[@id='s1']")
    login_btn.click()
    driver.get(hero_url)
    driver.close()
    print("Driver Closed")
    
  7. 此代码块在我的控制台上打印以下内容:

    Driver Closed