如何从输入字段中选择字符串/单词,同时单击粗体选项,以粗体形式生成字符串

2024-09-24 00:22:50 发布

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

我是使用python的selenium初学者

我在网页下面自动学习。 http://the-internet.herokuapp.com/iframe

在这里,我能够成功地在输入字段中输入文本。 之后,我想选择输入的文本并点击“B”更改格式

代码:

action_chain=ActionChains(self.driver)
inputframe=self.driver.find_element_by_xpath(self.XpathInputFrame)
action_chain.move_to_element(inputframe).perform()
inputframe.send_keys("Hello world") 
format1=self.driver.find_element_by_xpath("//i[@class='mce-ico mce-alignright']") 
action_chain.click(format1)

请帮助我如何更改格式


Tags: 文本selfchainby格式driverseleniumaction
1条回答
网友
1楼 · 发布于 2024-09-24 00:22:50

请尝试以下代码:

driver.get('http://the-internet.herokuapp.com/iframe')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'mce_0_ifr')))
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'body.mce-content-body > p')))
element.clear()
element.send_keys('Hello world')
element.send_keys(Keys.CONTROL + "A")
driver.switch_to.default_content()
driver.find_element_by_css_selector('i.mce-ico.mce-i-bold').click()

注意:如果您使用MAC OS,请将Keys.CONTROL更改为Keys.COMMAND

以下内容:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

相关问题 更多 >