gpt4 book ai didi

python - 如何获取网页中名称动态生成的元素

转载 作者:行者123 更新时间:2023-12-01 09:16:55 24 4
gpt4 key购买 nike

因此,我正在使用 python 和 selenium 制作一种网络抓取工具,我想要与之交互的下拉菜单之一根据网站上的其他交互被命名为不同的内容。它的名字是这样的:

<select id ="sel-abfj184h48cj" >

始终带有 sel- 前缀,其余部分动态生成。访问该元素的最佳方式是什么?我已经尝试过 selenium 的 find by id、name 和 XPath 方法,但有点卡住了。有任何想法吗?

最佳答案

CSS Selectors are string patterns used to identify an element based on a combination of HTML tag, id, class, and attributes. It is the most common locating strategy of advanced Selenium users because it can access even those elements that have no ID or name.

要匹配 id 的开头,您可以使用:(您的案例)

By.CssSelector("id^=sel-")

要匹配 id 的结尾,您可以使用:

By.CssSelector("id$=sel-")

使用包含:

By.cssSelector("[id*=sel-]")

关于python - 如何获取网页中名称动态生成的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51165060/

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