无法将Selenium Python中的send_keys发送到td(无可用输入标签)

时间:2019-03-08 17:10:24

标签: python selenium automation

我可以找到该元素并单击它:

driver.find_element_by_xpath("//*[@id='tinymce']/table/tbody/tr[2]/td[1]").click()

当我尝试通过以下方式向其发送密钥时:

driver.find_element_by_xpath("//*[@id='tinymce']/table/tbody/tr[2]/td[1]").send_keys("test")

我知道

    Traceback (most recent call last):
  File "C:\Users\jlesslar\Documents\Scripts\post robot automater.py", line 60, in <module>
    driver.find_element_by_xpath("//*[@id='tinymce']/table/tbody/tr[2]/td[1]").send_keys("test")
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 479, in send_keys
    'value': keys_to_typing(value)})
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
WebDriverException: Message: unknown error: cannot focus element
  (Session info: chrome=72.0.3626.81)
  (Driver info: chromedriver=2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387),platform=Windows NT 10.0.17134 x86_64)

据我所知,td是我要发送send_keys的位置,但在网上它说我应该有一个文本字段或输入标签可供我使用,以使send_keys正常工作(我不愿意)

enter image description here

指导将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:1)

您应该能够通过使用execute_script()的JS来更改innerHTML值

首先获取您的元素:

el = driver.find_element_by_xpath("//*[@id='tinymce']/table/tbody/tr[2]/td[1]")

然后尝试直接设置innerHTML值:

driver.execute_script('arguments[0].innerHTML = "test";', el)