javascript 我可以在 Python 和 Selenium 中使用正则表达式找到一个元素吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34315533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 17:32:44  来源:igfitidea点击:

Can I find an element using regex with Python and Selenium?

javascriptpythonhtmlregexselenium

提问by alecxe

I need to click a dropdown list and click a hidden element with in it. the html will be generated by javascript and I won't know the id or class name but I will know it will have a phrase in it. Can I find and element by regex and then click it with selenium?

我需要单击下拉列表并单击其中的隐藏元素。html 将由 javascript 生成,我不知道 id 或类名,但我知道它会有一个短语。我可以通过正则表达式查找和元素,然后用硒单击它吗?

回答by alecxe

You cannot simply do regex-based search with built-in selenium webdriver locators, but you have multiple things that might help you:

您不能简单地使用内置的 selenium webdriver 定位器进行基于正则表达式的搜索,但是您有多种方法可以帮助您:



There are also CSS selectorsfor partial match on element attributes:

还有用于元素属性部分匹配的CSS 选择器

a[href*=desiredSubstring]  # contains
a[href^=desiredSubstring]  # starts-with
a[href$=desiredSubstring]  # ends-with


And you can always find more elements than needed and filter them out later in Python, example:

你总是可以找到比需要更多的元素,然后在 Python 中过滤掉它们,例如:

import re

pattern = re.compile(r"^Some \w+ text.$")

elements = driver.find_elements_by_css_selector("div.some_class")
for element in elements:
    match = pattern.match(element.text)
    if match:
        print(element.text)

回答by MelArlo

You can use import reto perform regex functions. The snippet below looks through a table and grabs the text between the <b></b>tags in the first cell if the row has 3 cells in it.

您可以使用import re来执行正则表达式功能。<b></b>如果该行中有 3 个单元格,下面的代码段会查看一个表格并抓取第一个单元格中标签之间的文本。

import re
from lxml import html, etree

tree = html.fromstring(browser.page_source)
party_table = tree.xpath("//table")
assert len(party_table) == 1

CURRENT_PARTIES = []
for row in party_table[0].xpath("tbody/tr"):
    cells = row.xpath("td")
    if len(cells) != 3:
        continue

    if cells[1].text == "represented by":
        match = re.search(r'<b>(.+?)</b>', etree.tostring(cells[0]), re.IGNORECASE)
        print "MATCH: ", match