Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.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 硒;send_keys将字段保留为空,没有错误_Python_Selenium Webdriver_Selenium Chromedriver - Fatal编程技术网

Python 硒;send_keys将字段保留为空,没有错误

Python 硒;send_keys将字段保留为空,没有错误,python,selenium-webdriver,selenium-chromedriver,Python,Selenium Webdriver,Selenium Chromedriver,这里完全是初学者。我正在自动生成一张用于“不合规”罚单的表格,并成功地填写了除一个文本字段外的所有文本字段。问题是,我没有收到任何错误消息。代码一直到结尾,但留下一个文本字段为空。该文本框是票证的主体,用于说明问题,因此我不能不使用它来解决问题 以下是我最初编写的代码: print(desc_to_write) #Use while debuggin this to confirm the variable contain the string. desc = web.find_element

这里完全是初学者。我正在自动生成一张用于“不合规”罚单的表格,并成功地填写了除一个文本字段外的所有文本字段。问题是,我没有收到任何错误消息。代码一直到结尾,但留下一个文本字段为空。该文本框是票证的主体,用于说明问题,因此我不能不使用它来解决问题

以下是我最初编写的代码:

print(desc_to_write) #Use while debuggin this to confirm the variable contain the string.

desc = web.find_element_by_xpath('/html/body')
desc.send_keys(desc_to_write)
#No error but still nothing in the browser text field. 
xpath是我通过Chrome的“复制xpath”工具得到的

我也尝试过以下方法,但没有成功(相关评论是收到的错误)

#1:

#2:

#3:

#4:我也尝试过完整的Xpath,但没有成功。我必须自己猜测完整路径,因为“复制完整Xpath”工具将返回与上面相同的路径。我使用“iframe”的Xpath并添加“/html/body”获得了大部分路径。请注意,我对HTML一无所知,所以我可能在某个地方犯了一个错误。这是一次绝望的尝试

desc = web.find_element_by_xpath('//*[@id="NewRequestTab"]/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr[1]/td/table/tbody/tr[1]/td/table/tbody/tr[14]/td[2]/div/iframe/html/body')
desc.send_keys(desc_to_write)
由此产生的错误是:

#selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="NewRequestTab"]/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr[1]/td/table/tbody/tr[1]/td/table/tbody/tr[14]/td[2]/div/iframe/html/body"}  (Session info: chrome=88.0.4324.104)
我还附上了一个屏幕截图的代码中看到的铬的开发工具。我可以复制实际的代码,但我真的知道HTML是如何工作的,我不知道什么是相关的。如果需要,我可以提供更多。突出显示的部分是Chrome开发工具中inspector工具的结果

谢谢你的帮助


您必须切换到iframe,才能将其内容与selenium一起使用。 最好的方法是先等待它出现,然后切换:

通过导入从selenium.webdriver.common.by导入 从selenium.webdriver.support.ui导入WebDriverWait 从selenium.webdriver.support将预期的_条件导入为EC wait=WebDriverWait(驱动程序,300) 等待.until(EC.frame_to_be_available_和_switch_to_it(driver.find_元素(By.CLASS_NAME,'textarea'))
我根据John answer做了一些研究,发现了以下几点。然后我想到了

web.switch_to.frame(web.find_element_by_xpath('//*[@id="NewRequestTab"]/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr[1]/td/table/tbody/tr[1]/td/table/tbody/tr[14]/td[2]/div/iframe'))
desc = web.find_element_by_class_name('editor_body')
desc.click()
desc.send_keys(desc_to_write)
web.switch_to.default_content()

现在一切都好了。谢谢

如果元素在iframe中,我相信您必须显式地切换到框架;您无法从包含页面访问其内容。您的评论让我做了一些研究,我做了一些研究,发现了这一点,并能够更改代码以使其在我的脚本上工作。谢谢
desc = web.find_element_by_xpath('//*[@id="NewRequestTab"]/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr[1]/td/table/tbody/tr[1]/td/table/tbody/tr[14]/td[2]/div/iframe/html/body')
desc.send_keys(desc_to_write)
#selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="NewRequestTab"]/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr[1]/td/table/tbody/tr[1]/td/table/tbody/tr[14]/td[2]/div/iframe/html/body"}  (Session info: chrome=88.0.4324.104)
web.switch_to.frame(web.find_element_by_xpath('//*[@id="NewRequestTab"]/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr[1]/td/table/tbody/tr[1]/td/table/tbody/tr[14]/td[2]/div/iframe'))
desc = web.find_element_by_class_name('editor_body')
desc.click()
desc.send_keys(desc_to_write)
web.switch_to.default_content()