<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>技术笔记 &#8211; 李辉 / Grey Li</title>
	<atom:link href="https://greyli.com/category/%E6%8A%80%E6%9C%AF%E7%AC%94%E8%AE%B0/feed/" rel="self" type="application/rss+xml" />
	<link>https://greyli.com</link>
	<description>一个编程和写作爱好者的在线记事本</description>
	<lastBuildDate>Thu, 06 Nov 2025 11:36:11 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.9.26</generator>

<image>
	<url>https://greyli.com/wp-content/uploads/2025/03/avatar-500-compressed-144x144.jpg</url>
	<title>技术笔记 &#8211; 李辉 / Grey Li</title>
	<link>https://greyli.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Flask test_client()测试客户端为勾选框传递布尔值数据</title>
		<link>https://greyli.com/flask-test_client-pass-boolean-data/</link>
		<comments>https://greyli.com/flask-test_client-pass-boolean-data/#respond</comments>
		<pubDate>Sat, 28 Jul 2018 10:28:57 +0000</pubDate>
		<dc:creator><![CDATA[李辉]]></dc:creator>
				<category><![CDATA[技术笔记]]></category>
		<category><![CDATA[Flask]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Web开发]]></category>
		<category><![CDATA[单元测试]]></category>

		<guid isPermaLink="false">http://greyli.com/?p=1820</guid>
		<description><![CDATA[今天写单元测试发现了一个常见的问题，即测试时发送POST请求时如何传入布尔值数据（勾选框字段值）？答案是：你没 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>今天写单元测试发现了一个常见的问题，即测试时发送POST请求时如何传入布尔值数据（勾选框字段值）？答案是：你没法直接传递布尔值。其实这个答案相当显而易见，客户端当然没法向服务器端发送Python类型的数据，数据的转换是在接受到请求数据后在服务器端进行的。之前在不借助Flask-WTF/WTForms，手动编写表单并处理时就已经注意到了这个问题，不过在测试中不太容易想到。</p>
<p>首先，我们需要了解一下勾选框（<code>&lt;input type="checkbox"&gt;</code>）提交的行为：</p>
<ul>
<li>如果没有勾选，那么勾选框字段的值为空值，而且这个字段不会被序列化到请求中；在服务器端，WTForms会将其转换为<code>False</code>。</li>
<li>如果勾选框被勾选，那么传入服务器端的数据会是该字段value属性的值，如果value属性的值为空，那么则提交字符串<code>"on"</code>；在服务器端，WTForms会将其转换为<code>True</code>。</li>
</ul>
<p>也就是说，勾选框的数据只要不为空，WTForms就会将其转换为True。所以，在测试中，如果你想让勾选框的值最终转换为True，那么就传入任意字符串；反之则传递空字符串或直接不加入该字段。下面是传入空字符串的示例：</p>
<pre class="">def test_privacy_setting(self):
    self.login()
    response = self.client.post(url_for('user.privacy_setting'), data=dict(
        public_collections='',  # &lt;--
    ), follow_redirects=True)

    user = User.query.get(1)
    self.assertEqual(user.public_collections, False)</pre>
<p>顺便说一句，基于勾选框的提交行为，如果没有使用Flask-WTF/WTForms，那么在手动处理提交数据的时候也要进行相应的处理：没有在<code>request.form</code>中获取到勾选框字段（比如，<code>request.form.get('remember')</code>会是<code>None</code>），即表示没有勾选，那么就转换为False；勾选框字段一旦出现，那么就表示勾选，转换为<code>True</code>。</p>
]]></content:encoded>
			<wfw:commentRss>https://greyli.com/flask-test_client-pass-boolean-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>在Python Selenium中为Chrome和Firefox浏览器开启headless模式</title>
		<link>https://greyli.com/python-selenium-headless-for-chrome-firefox/</link>
		<comments>https://greyli.com/python-selenium-headless-for-chrome-firefox/#comments</comments>
		<pubDate>Sat, 28 Jul 2018 09:51:17 +0000</pubDate>
		<dc:creator><![CDATA[李辉]]></dc:creator>
				<category><![CDATA[技术笔记]]></category>
		<category><![CDATA[Chrome]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Selenium]]></category>

		<guid isPermaLink="false">http://greyli.com/?p=1814</guid>
		<description><![CDATA[我们通常会使用Selenium编写UI测试，为浏览器开启Headless模式（执行操作时不显示GUI窗口）会很 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>我们通常会使用Selenium编写UI测试，为浏览器开启Headless模式（执行操作时不显示GUI窗口）会很方便。最新版本的Chrome和Firefox中，均已支持headless模式。</p>
<p>在Selenium中，为这两个浏览器开启headless模式的方式基本相同：</p>
<p>Chrome：</p>
<pre class="">from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome(options=options)</pre>
<p>Firefox：</p>
<pre class="">from selenium import webdriver

options = webdriver.FirefoxOptions()
options.add_argument('headless')
driver = webdriver.Firefox(options=options)</pre>
<p>我提交的PR<a href="https://github.com/SeleniumHQ/selenium/pull/5120" rel="noreferrer">#5120</a>添加了和Chrome相同的导入接口，如果你使用Selenium小于3.8.0版本，则需要将上面的<code>webdriver.FirefoxOptions()</code>替换为<code>webdriver.firefox.options.Options()</code>。</p>
<p>另外，你也可以使用环境变量<code>MOZ_HEADLESS</code> 来为Firefox开启headless模式：</p>
<pre class="">import os
from selenium import webdriver

os.environ['MOZ_HEADLESS'] = '1'  # &lt;- this line
driver = webdriver.Firefox()</pre>
<p>本文基于我在Stack Overflow的这篇回答：<a href="https://stackoverflow.com/a/47481793/5511849">https://stackoverflow.com/a/47481793/5511849</a></p>
]]></content:encoded>
			<wfw:commentRss>https://greyli.com/python-selenium-headless-for-chrome-firefox/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>使用Python创建随机文件名</title>
		<link>https://greyli.com/python-random-filename/</link>
		<comments>https://greyli.com/python-random-filename/#respond</comments>
		<pubDate>Wed, 25 Jul 2018 02:08:44 +0000</pubDate>
		<dc:creator><![CDATA[李辉]]></dc:creator>
				<category><![CDATA[技术笔记]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://greyli.com/?p=1812</guid>
		<description><![CDATA[当用户上传文件时，为了进行统一处理，我们可以为所有文件创建随机文件名。Python标准库中的uuid模块很适合 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>当用户上传文件时，为了进行统一处理，我们可以为所有文件创建随机文件名。Python标准库中的<code>uuid</code>模块很适合用来生成文件名。我一般会使用<code>uuid.uuid4().hex</code>作为随机文件名，因为它会返回16进制字符串形式的uuid，并且不包含连字符，更易读：</p>
<pre class="">import uuid
filename = uuid.uuid4().hex</pre>
<p>和其他常见方式的输出对比：</p>
<pre class="">&gt;&gt;&gt; import uuid
&gt;&gt;&gt; uuid.uuid()
UUID('20818854-3564-415c-9edc-9262fbb54c82')
&gt;&gt;&gt; str(uuid.uuid4())
'f705a69a-8e98-442b-bd2e-9de010132dc4'
&gt;&gt;&gt; uuid.uuid4().hex
'5ad02dfb08a04d889e3aa9545985e304'  # &lt;-- this one</pre>
<p>这篇文章原发于我在Stack Overflow上的这个回答：<a href="https://stackoverflow.com/a/44992275/5511849">https://stackoverflow.com/a/44992275/5511849</a></p>
]]></content:encoded>
			<wfw:commentRss>https://greyli.com/python-random-filename/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
