如何通过webdriver在浏览器屏幕上选择一行文本来使用键盘或鼠标?

2024-09-30 01:34:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用ChromeDriver在浏览器屏幕中选择一行文本并更改样式。例如,在编辑器工具栏中单击粗体按钮。你知道吗

例如,它是我的html代码。你知道吗

<p id="boldId">bold </p>

我附上了图片。enter image description here


Tags: 代码文本id屏幕html浏览器图片样式
1条回答
网友
1楼 · 发布于 2024-09-30 01:34:23

要创建文本选择、设置格式并获得HTML结果吗?你知道吗

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("http://haixing-hu.github.io/vue-html-editor/demo.html")

editable = driver.find_element_by_xpath('//div[@class="note-editable panel-body"]')
buttonBold = driver.find_element_by_xpath('//button[@data-name="bold"]')
buttonItalic = driver.find_element_by_xpath('//button[@data-name="italic"]')

actions = webdriver.ActionChains(driver)
actions.move_to_element(editable)
actions.click()
actions.send_keys(Keys.CONTROL + 'a')
actions.click(buttonBold) 
actions.click(buttonItalic)
actions.perform()

print('HTML Source:')
print(editable.get_attribute('innerHTML'))

# <span style="font-weight: bold; font-style: italic;">Hello World!</span>

相关问题 更多 >

    热门问题