首页 > 解决方案 > 有没有办法在 python selenium 中使用 send_keys 更改文本的颜色?

问题描述

我在 python 中有一个脚本(web-scraper)——也使用selenium——在某些时候我必须填写一些字段。在我填写之后,我附上了一个带有lorem模块的随机段落。关键是我想隐藏这一段,例如,给它一些透明度或将颜色更改为白色(因为背景是白色的)。

我的代码是这样的:

# import selenium, lorem, create the webdriver object,
# get the url, etc.
driver.find_element_by_id('some_id').send_keys(
        'some_string' + lorem.paragraph())

lorem.paragraph()那么,有什么方法可以在不影响第一个字符串的易读性的情况下将颜色(或使其透明)更改为第二个字符串?

我是 python selenium 的新手,如果有人能提供帮助,我将不胜感激。提前致谢。任何答案将不胜感激。

标签: pythonseleniumweb-scrapingcolors

解决方案


您可以创建一个函数来将 HTML 标记附加到您的文本中。就像是:

def hide(text): 
  return '<h1 style="color:white">' + text + '</h1>'

然后用它来美白你的文字

# import selenium, lorem, create the webdriver object,
# get the url, etc.
driver.find_element_by_id('some_id').send_keys(
        'some_string' + hide(lorem.paragraph()))

推荐阅读