gpt4 book ai didi

With selenium, Is it possible to identify a dynamic web element selector that has a specific pattern?(使用Selify,是否有可能识别具有特定模式的动态Web元素选择器?)

转载 作者:bug小助手 更新时间:2023-10-24 22:19:58 25 4
gpt4 key购买 nike



I want to find a web element by a CSS selector, but the Selectors change each time the page is loaded and adhere to a certain pattern:

我想通过一个css选择器找到一个网页元素,但每次加载页面时选择器都会改变,并遵循一定的模式:


The selector is always 10 characters long

选择符的长度始终为10个字符


The 2nd character is always an integer between 1-9

第二个字符始终是1-9之间的整数


the 7th character is always a capital letter

第7个字符始终是大写字母


The 10th character is always a lowercase letter between a-g

第10个字符始终是a-g之间的小写字母


e.g.

例如:


a8DFf3Ac3e

A8DFf3Ac3e


z1Az5fE52b

Z1Az5fE52b


Is it possible to tell selenium webdriver that the element appeared if it sees a CSS selector that matches this pattern?

有没有可能告诉SelensWebDriver,如果它看到一个与此模式匹配的CSS选择器,元素就会出现?


I am using selenium webdriver with Python

我正在使用带有PYTHON的Selify WebDriver


更多回答

What are those? classes, ids, or something else?

那些是什么?班级、身份证或其他东西?

They are ids id=

它们是ID id=

优秀答案推荐

This is not possible with a CSS selector alone, but a JS script would be sufficient:

仅使用CSS选择器是不可能的,但JS脚本就足够了:


elements = driver.execute_script(
'''
return [...document.querySelectorAll('[id]')]
.filter(element => element.id.match(/^.[1-9]....[A-Z].[a-g]$/))
'''
)

The snippet above selects every element that has an id which matches the regex ^.[1-9]....[A-Z].[a-g]$, JavaScript way. You can, of course, do the same in Python with By.CSS_SELECTOR:

上面的代码片断选择id与正则表达式^.[1-9]...[A-Z].[a-g]$匹配的每个元素。当然,您也可以在Python中使用By.css_selector执行相同的操作:


elements_with_id = driver.find_elements(By.CSS_SELECTOR, '[id]')
elements_with_matching_id = [
element for element in elements_with_id
if re.fullmatch(r'.[1-9]....[A-Z].[a-g]', element.get_attribute('id'))
]

They work (mostly) the same. Your choice.

它们的工作原理(基本上)是一样的。你自己选吧。


更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com