Python 在selenium中查找具有生成Id的元素?

Python 在selenium中查找具有生成Id的元素?,python,html,selenium,Python,Html,Selenium,HTML: 因此,页面生成上述内容,Id的数字部分在每次加载时都是不同的 如何使用selenium和python定位它们,或者甚至定位一组与模式匹配的它们?使用XPath,如下所示: <g id="OpenLayers.Layer.Vector_101_vroot"> <image id="OpenLayers.Geometry.Point_259_status"..></image> 希望有帮助 无需进行数据和模式匹配,您可以使用名为“Beau

HTML:


因此,页面生成上述内容,Id的数字部分在每次加载时都是不同的

如何使用selenium和python定位它们,或者甚至定位一组与模式匹配的它们?

使用XPath,如下所示:

<g id="OpenLayers.Layer.Vector_101_vroot">
    <image id="OpenLayers.Geometry.Point_259_status"..></image>

希望有帮助

无需进行数据和模式匹配,您可以使用名为“Beauty soup”的模块和一些简单的文档

例如,要获取id=“OpenLayers.Layer.Vector\u 101\u vroot”的所有标记,请使用:

soup=BeautifulSoup()
soup.find_all(id=“OpenLayers.Layer.Vector_101_vroot”)
根据,您可以使用。 下面的代码单击
id
属性中包含
OpenLayers.Layer.Vector
的元素

蟒蛇

soup = BeautifulSoup(<your_html_as_a_string>)
soup.find_all(id="OpenLayers.Layer.Vector_101_vroot")
HTML(显示在
http://localhost:1111/

xxx

如果我已经拥有包含该元素的元素,我应该使用//还是。//?感谢您可以使用://g[contains(@id,'OpenLayers.Layer.Vector')]/image[contains(@id,'OpenLayers.Geometry.Point')]感谢您的回复,但BeautifulSoup似乎是一个解析器,我正在使用selenium,它可以像人一样与浏览器交互。谢谢
soup = BeautifulSoup(<your_html_as_a_string>)
soup.find_all(id="OpenLayers.Layer.Vector_101_vroot")
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('http://localhost:1111/')
browser.find_element_by_css_selector('[id*="OpenLayers.Layer.Vector"]').click()
<button id="OpenLayers.Layer.Vector_123" onclick="alert(1);return false">xxx</button>