硒:无法通过键盘到达

时间:2019-09-18 22:14:49

标签: python selenium

我尝试使用python / selenium在gist.github.com上发布gist, 这是我当前的代码:

driver.find_element(By.NAME, "gist[contents][][name]").send_keys("file.md")

#import pdb; pdb.set_trace()

tmp = driver.find_element(By.NAME, "gist[contents][][value]").send_keys("Description file")
#keyboard.write("Description file")


driver.find_element(By.XPATH, "//button[@name=\'gist[public]\']").click()

time.sleep(30)

代码是临时工作的,我在gist[contents][][name]输入上遇到了大问题,应用程序将其杀死并返回此错误:is not reachable by keyboard ...我不知道如何解决此错误,有谁知道我该如何解决这个问题?

完全错误:

admin$ python github_login.py 
Traceback (most recent call last):
  File "github_login.py", line 31, in <module>
    tmp = driver.find_element(By.NAME, "gist[contents][][value]").send_keys(".")
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 479, in send_keys
    'value': keys_to_typing(value)})
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: Element <textarea class="form-control file-editor-textarea js-blob-contents js-code-textarea " name="gist[contents][][value]"> is not reachable by keyboard

1 个答案:

答案 0 :(得分:0)

您可以使用此定位器:

.find_element(By.CLASS_NAME, "CodeMirror-lines")

还要使用ActionChains

首先,您需要进行以下导入:

from selenium.webdriver import ActionChains

尝试以下代码:

driver.get("https://gist.github.com/")
driver.find_element(By.NAME, "gist[description]").click()
driver.find_element(By.NAME, "gist[description]").send_keys("GIST DESCRIPTION")
driver.find_element(By.NAME, "gist[contents][][name]").send_keys("file.md")

#HERE
el = driver.find_element(By.CLASS_NAME, "CodeMirror-lines")
ActionChains(driver).move_to_element(el).click(el).send_keys('Description file').perform()