LINK_TEXT 是大写的,但在浏览器的 html 代码中显示为小写
LINK_TEXT is capitalized but shows in lower case in browser's html code
在this page上,LINK_TEXT'DAILY TREASURY PAR YIELD CURVE RATES'明显是大写的。但是,在 Chrome 中按 Ctrl-U 后生成的 html 中显示为小写 'Daily Treasury PAR Yield Curve Rates'。它在 driver.page_source 中以相同的方式显示,其中驱动程序是 selenium 的 webdriver,以及在通过 Beautiful Soup page_source 之后。
但是,只有当 LINK_TEXT 像在浏览器中一样全部大写时,selenium 才能识别该元素。
这是怎么回事?
这是因为应用于页面上 h3
元素的 CSS 使其大写:
h3 {
text-transform: uppercase !important;
letter-spacing: 2px !important;
line-height: 1.18 !important;
font-weight: 700 !important;
margin-top: 20px !important;
font-size: 24px !important;
color: #0053a3 !important;
margin: 20px 0 !important; }
WebDriver 的 Get Element Text 命令将抓取呈现的文本 ,这将受到任何影响 CSS 规则正在作用于它。
长话短说,这是 Selenium 中的预期行为。
参考:https://w3c.github.io/webdriver/webdriver-spec.html#get-element-text
在this page上,LINK_TEXT'DAILY TREASURY PAR YIELD CURVE RATES'明显是大写的。但是,在 Chrome 中按 Ctrl-U 后生成的 html 中显示为小写 'Daily Treasury PAR Yield Curve Rates'。它在 driver.page_source 中以相同的方式显示,其中驱动程序是 selenium 的 webdriver,以及在通过 Beautiful Soup page_source 之后。
但是,只有当 LINK_TEXT 像在浏览器中一样全部大写时,selenium 才能识别该元素。
这是怎么回事?
这是因为应用于页面上 h3
元素的 CSS 使其大写:
h3 {
text-transform: uppercase !important;
letter-spacing: 2px !important;
line-height: 1.18 !important;
font-weight: 700 !important;
margin-top: 20px !important;
font-size: 24px !important;
color: #0053a3 !important;
margin: 20px 0 !important; }
WebDriver 的 Get Element Text 命令将抓取呈现的文本 ,这将受到任何影响 CSS 规则正在作用于它。
长话短说,这是 Selenium 中的预期行为。
参考:https://w3c.github.io/webdriver/webdriver-spec.html#get-element-text