You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用正则表达式定位后跟.send_keys的b.element?

Solution to Target b.element When Followed by .send_keys

Got it, I see exactly what you're aiming for—you want to isolate just the b.element portion whenever it's followed by a .send_keys call, instead of matching the full b.element(...).send_keys string.

The trick here is using a positive lookahead assertion in your regex. This lets you verify that a specific pattern comes right after your target text, without including that trailing pattern in your final match result.

The Regex You Need

b\.element(?=\(.*\)\.send_keys)

Let’s break this down step by step:

  • b\.element: Matches the literal string b.element (we escape the dot with \ because dots act as wildcards in regex by default).
  • (?=\(.*\)\.send_keys): This is the positive lookahead. It checks that immediately after b.element, there’s:
    • An opening parenthesis \(
    • Any sequence of characters (the arguments inside your element call) .*
    • A closing parenthesis \)
    • Followed directly by .send_keys

Example Usage

If your input string is:

b.element(By.ID, "username_input").send_keys("my_username")

Running the regex above will match only b.element, not the rest of the line.

Bonus: Capture the Full Element Call (Without .send_keys)

If you ever need to grab the entire b.element(...) part (excluding .send_keys), you can tweak the regex to:

b\.element\(.*\)(?=\.send_keys)

This would match b.element(By.ID, "username_input") in the example above.

内容的提问来源于stack exchange,提问作者RAJ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:59:48