Selenium / Instagram:send_keys()不再工作了

时间:2018-01-26 10:56:14

标签: python selenium

我在python中制作了一个Instagram机器人,并且由于某种原因,它过去一直工作到今天。当我将用户名和密码传递给send_keys()函数时,它告诉我键盘无法访问该元素。这是完整的错误消息:

  

追踪(最近一次通话):     文件" main.py",第28行,in       login_insta(driver,usr,pwd)     文件" /home/unknown/Documents/code/python/instats/login_profile.py" ;,第14行,在login_insta中       usr_box.send_keys(用户名)     文件" /home/unknown/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webelement.py",第352行,在send_keys中       '值&#39 ;: keys_to_typing(value)})     文件" /home/unknown/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webelement.py" ;,第501行,在_execute中       return self._parent.execute(command,params)     文件" /home/unknown/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py" ;,第311行,执行       self.error_handler.check_response(响应)     文件" /home/unknown/.local/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py" ;,第237行,在check_response中       提出exception_class(消息,屏幕,堆栈跟踪)   selenium.common.exceptions.ElementNotInteractableException:消息:键盘无法访问元素

这是不起作用的功能:

def login_insta(driver, username, password):
    login = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "/html/body/span/section/main/article/div[2]/div[2]/p/a")))
    login.click()

    usr_box = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "/html/body/span/section/main/article/div[2]/div[1]/div/form/div[1]/div/div[1]/label")))
    pwd_box = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "/html/body/span/section/main/article/div[2]/div[1]/div/form/div[2]/div/div[1]/label")))

    usr_box.send_keys(username)
    pwd_box.send_keys(password)

    login_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "/html/body/span/section/main/article/div[2]/div[1]/div/form/span/button")))
    login_button.click()

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

当页面代码发生微小变化时,像这样的长XPath很容易破坏。

尝试使用这些XPath:

'//input[@type="text"]' // username or phone number
'//input[@type="password"]' // password
'//button[contains(text(), "Log in")]' // log in button