Tomy
Tomy

Reputation: 67

Finding a element with a generated Id in selenium?

HTML:

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

So the page generates the above and the number section of the Id is different on each load.

How do I located them, or even a group of them that match the pattern using selenium and python?

Upvotes: 1

Views: 450

Answers (3)

ymonad
ymonad

Reputation: 12090

according to this answer, you can use css3 substring matching attribute selector. the following code clicks an element which contains OpenLayers.Layer.Vector in id attribute.

Python

from selenium import webdriver
browser = webdriver.Chrome()
browser.get('http://localhost:1111/')
browser.find_element_by_css_selector('[id*="OpenLayers.Layer.Vector"]').click()

HTML (which is displayed in http://localhost:1111/)

<button id="OpenLayers.Layer.Vector_123" onclick="alert(1);return false">xxx</button>

Upvotes: 0

Ishan Khare
Ishan Khare

Reputation: 1767

no need for and pattern matching, you can use this module here called "beautiful soup" with some easy documentation here.

for example to get all tags with id="OpenLayers.Layer.Vector_101_vroot" use:

soup = BeautifulSoup(<your_html_as_a_string>)
soup.find_all(id="OpenLayers.Layer.Vector_101_vroot")

Upvotes: 0

Sitam Jana
Sitam Jana

Reputation: 3129

Use Xpaths like below:

//g[contains(@id, 'OpenLayers.Layer.Vector')]
//image[contains(@id, 'OpenLayers.Geometry.Point')]

Hope if helps!

Upvotes: 1

Related Questions