我正在尝试使用python和selenium-webdriver替换contenteditable =“true”div标签中的章节标题,起初我正在搜索章节标题,通常是在第一行...然后我' m用空值替换它并保存..但刷新浏览器后它没有保存。但我看到代码正在运行。这是我的代码
##getting content editable div tag
input_field = driver.find_element_by_css_selector('.trumbowyg-editor')
### getting innerHTML of content editable div
chapter_html = input_field.get_attribute('innerHTML')
chapter_content = input_field.get_attribute('innerHTML')
if re.search('<\w*>', chapter_html):
chapter_content = re.split('<\w*>|</\w*>', chapter_html)
first_chapter = chapter_content[1]
### replacing first_chapter with ''
chapter_replace = chapter_html.replace(first_chapter, '')
### writing back innerHTML without first_chapter string
driver.execute_script("arguments[0].innerHTML = arguments[1];",input_field, chapter_replace)
time.sleep(1)
## click on save button
driver.find_element_by_css_selector('.btn.save-button').click()
我该如何处理?它在我手动操作时工作(我的意思是它可能不是网站问题/错误)...请帮忙...
相关HTML如下:
<div class="trumbowyg-editor" dir="ltr" contenteditable="true">
<p>Chapter 1</p>
<p> There is some text</p>
<p> There is some text</p>
<p> There is some text</p>
</div>
答案 0 :(得分:0)
根据您共享的HTML以用空值替换章节标题,您必须将 WebDriverWait 与expected_conditions
子句设置为visibility_of_element_located
,并且可以使用以下块代码:
page_number = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='trumbowyg-editor' and @contenteditable='true']/p[contains(.,'Chapter')]")))
driver.execute_script("arguments[0].removeAttribute('innerHTML')", page_number)
#or
driver.execute_script("arguments[0].removeAttribute('innerText')", page_number)
#or
driver.execute_script("arguments[0].removeAttribute('textContent')", page_number)