DEV Community

Discussion on: A Practical Guide for Finding Elements with Selenium

Collapse
 
frostbrewed profile image
Frostbrewed • Edited

Check your formatting, you are missing the end parenthesis.

videoFrame = driver.find_element_by_xpath("//*[contains(@id ,'ls_embed')]")

Other ways is to choose the complete xpath instead of the id of the element. When you right click on the element in the DOM you can choose a various number of options, for XPath there are 2, XPath and full XPath. By choosing full XPath it will return the XPath from the root, by using this it will not matter what the id of the element is.

Another way to go about this is to find the closest parent element that has a static locator you can find it by then proceed down the node list using a known locator or attribute of the element that you need.

For example, if the element that you are looking for is a div with the dynamic id and it has a specific attribute, maybe take a given class for example

The example below I know it is a "div"

videoframe = driver.find_element_by_xpath("//*[@id ='idOfKnownElement']//div[@class='title-row']")

if not you can use "*" to do a search no matter what the type of node as long as it has the class you are looking for.

videoframes = driver.find_elements_by_xpath("//[@id ='idOfKnownElement']//[@class='title-row']")

videoframe = videoframes[x]

One word of caution doing this is going to return a list of elements that contain the attribute that you are searching for if there is more then one. You will have to either be selective when choosing the attribute so that only one is returned or know that you need to select it from a list of returned elements. it is easy if it is always only one returned, it is going to be item 0.

edited, removed extra "" thought they would bold the text and added a comment about the missing parenthesis