Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Can';t使用Selenium发送_keys()到WYSWYG编辑器_Python_Html_Selenium - Fatal编程技术网

Python Can';t使用Selenium发送_keys()到WYSWYG编辑器

Python Can';t使用Selenium发送_keys()到WYSWYG编辑器,python,html,selenium,Python,Html,Selenium,我正在尝试将文章自动发布到我的博客,该博客由Naver和Selenium提供服务 问题是我无法将文本放入编辑器。 我曾尝试使用发送密钥(),但它不起作用。 这是我尝试过的Python Selenium代码 driver = webdriver.Chrome('./chromedriver') driver.get('https://section.blog.naver.com') driver.find_element_by_xpath("//a[@class = 'login_but

我正在尝试将文章自动发布到我的博客,该博客由Naver和Selenium提供服务

问题是我无法将文本放入编辑器。 我曾尝试使用
发送密钥()
,但它不起作用。 这是我尝试过的Python Selenium代码

driver = webdriver.Chrome('./chromedriver')    

driver.get('https://section.blog.naver.com')
driver.find_element_by_xpath("//a[@class = 'login_button']").click()
time.sleep(1)

driver.execute_script("document.getElementsByName('id')[0].value=\'" + user_id + "\'")
driver.execute_script("document.getElementsByName('pw')[0].value=\'" + password + "\'")
driver.find_element_by_xpath("//input[@title = 'Sign in']").click()
time.sleep(1)

driver.find_element_by_xpath("//a[@bg-nclick = 'hmp*s.write']").click()
time.sleep(1)

driver.switch_to.window(driver.window_handles[-1])
driver.switch_to.frame('mainFrame')

driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div[2]/div/div/div/div[1]/article/div/header/button").click()
driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div[2]/div/div/div/div[1]/div[2]/section/article/div[1]/div").click()

#This line is being a problem.
driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div[2]/div/div/div/div[1]/div[2]/section/article/div[1]/div[1]/div/div/p/span").send_keys('Heading1')
它抛出异常,如下所示

---------------------------------------------------------------------------
ElementNotInteractableException           Traceback (most recent call last)
<ipython-input-50-e483e0f23c79> in <module>
     25 driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div[2]/div/div/div/div[1]/article/div/header/button").click()
     26 driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div[2]/div/div/div/div[1]/div[2]/section/article/div[1]/div").click()
---> 27 driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div[2]/div/div/div/div[1]/div[2]/section/article/div[1]/div[1]/div/div/p/span").send_keys('Heading1')
     28 #driver.execute_script("document.getElementByXpath('/html/body/div[1]/div/div[2]/div[2]/div/div/div/div[1]/div[2]/section/article/div[1]/div[1]/div/div/p/span').value='Heading1'")

~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py in send_keys(self, *value)
    477         self._execute(Command.SEND_KEYS_TO_ELEMENT,
    478                       {'text': "".join(keys_to_typing(value)),
--> 479                        'value': keys_to_typing(value)})
    480 
    481     # RenderedWebElement Items

~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py in _execute(self, command, params)
    631             params = {}
    632         params['id'] = self._id
--> 633         return self._parent.execute(command, params)
    634 
    635     def find_element(self, by=By.ID, value=None):

~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
    319         response = self.command_executor.execute(driver_command, params)
    320         if response:
--> 321             self.error_handler.check_response(response)
    322             response['value'] = self._unwrap_value(
    323                 response.get('value', None))

~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):

ElementNotInteractableException: Message: element not interactable
---------------------------------------------------------------------------
ElementNotInteractiableException回溯(最近一次调用)
在里面
25驱动程序。通过xpath(“html/body/div[1]/div/div[2]/div[2]/div/div/div/div/div[1]/article/div/header/button”)查找元素。单击()
26驱动程序。通过xpath(“/html/body/div[1]/div/div[2]/div[2]/div/div/div/div[1]/div[2]/section/article/div[1]/div”)查找元素。单击()
--->27驱动程序。通过xpath(“/html/body/div[1]/div/div[2]/div[2]/div/div/div[1]/div[2]/section/article/div[1]/div[1]/div/div/p/span”)查找元素。发送密钥('Heading1')
28驱动程序。执行脚本(“document.getElementByXpath('/html/body/div[1]/div/div[2]/div[2]/div/div/div/div[1]/div[2]/section/article/div[1]/div[1]/div[1]/div/p/span')。value='Heading1')
发送密钥中的~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py(self,*value)
477 self.\u execute(Command.SEND\u KEYS\u到\u元素,
478{'text':“”.join(键到键入(值)),
-->479“值”:键到键入(值)})
480
481#呈现的WebElement项
~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py in_execute(self、command、params)
631参数={}
632参数['id']=self.\u id
-->633返回self.\u parent.execute(命令,参数)
634
635 def find_元素(self,by=by.ID,value=None):
执行中的~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py(self,driver_命令,参数)
319 response=self.command\u executor.execute(driver\u command,params)
320如果响应:
-->321自我错误处理程序检查响应(响应)
322响应['value']=self.\u展开值(
323响应。获取('值',无))
检查响应中的~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py(self,response)
240警报文本=值['alert']。获取('text')
241引发异常类(消息、屏幕、堆栈跟踪、警报文本)
-->242引发异常类(消息、屏幕、堆栈跟踪)
243
244定义值或默认值(self、obj、key、default):
ElementNotInteractiableException:消息:元素不可交互
我知道“ElementNotInteractivatableException:Message:element NotInteractivatable”是什么意思。 虽然我尝试了所有可能的Xpath来找到一个可以使用
send_keys()
的元素,但我失败了;它后面跟着同样的例外情况

所以我研究了web编辑器的源代码。它以一种不熟悉的方式工作,通过使用iframe和大量具有自动生成id的元素。这就是编辑器的样子

下面是一个简短的视频,可能会让你对编辑器的工作原理有更多的了解

如果你有什么想法,我应该尝试,请让我知道

*编辑

正如您在python代码中看到的,我正在考虑使用javascript实现web自动化。然而,问题是
span
元素,我怀疑它作为
input
元素,具有自动生成的随机ID。唯一的可能性是使用Xpath选择它,但不幸的是,Javascript没有
getElementByXpath()


编辑1-Javascript方法

您可以尝试使用JavaScript将文本发送到
span
元素

以下是一个示例:

driver.executeScript("document.getElementById('elementID').setAttribute('value', 'yourTextHere')");

可以将“elementId”替换为跨度元素的ID

使用XPath编辑2-Javascript方法

我知道您的
span
包含动态ID,除了XPath之外,其他任何方法都无法找到它。对于这个解决方案,我们可以使用它的XPath获取
span
,然后获取它的ID以传递给Javascript方法

IWebElement textAreaElement = driver.findElement(By.XPath("//article//span"));
string textAreaElementId = textAreaElement.GetAttribute("id");
driver.executeScript("document.getElementById('" + textAreaElementId + "').setAttribute('value', 'yourTextHere')");
在本示例中,您使用XPath查找
span
元素,然后获取其ID属性。将ID属性传递到JS函数中,以正确定位span元素

或者,您可以尝试以下方法:

IWebElement textAreaElement = driver.findElement(By.XPath("//article//span"));
driver.executeScript("arguments[0].setAttribute('value', 'yourTextHere')", textAreaElement);
在本例中,您将元素本身直接传递到JS函数中

附加编辑

我注意到你对一些定位器使用绝对XPath。如果您想简化它们,您可以像上面使用的那样使用相对XPath表示法:
/
。我将为您提供一些可以简化的XPath示例:

绝对XPath#1

相对XPath#1

绝对XPath#2

相对XPath#2


使用XPath不需要从文档的
html
节点的顶部开始。您可以使用
/
符号从页面上的任何位置启动XPath。

如果span绝对是正确的元素,您可以尝试使用JavaScript发送文本。我将用这个解决方案为你编辑我的答案。克里斯汀,我刚刚用另一个截图编辑了我的问题。我考虑过这个方法,但很难指定元素。@ChanwooAhn谢谢你提供额外的屏幕截图,这对我很有帮助。我已经用Javascript解决方案更新了我的答案,该解决方案可以通过XPath获取
span
,并使用其ID调用Javascript。我还在底部为您添加了一些XPath提示。我希望这有帮助。
driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div[2]/div/div/div/div[1]/article/div/header/button").click()
driver.find_element_by_xpath("//article/div/header/button").click()
driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div[2]/div/div/div/div[1]/div[2]/section/article/div[1]/div").click()
driver.find_element_by_xpath("//article/div[1]/div").click()