I am using Selenium in python to find some elements in a DOM tree. I want to find class names having a certain pattern. What can I do to find such class names? I want to use regex with find_elements_by_class_name
but I don't know how I could be doing that. Please suggest what should I do.
For example: I want to do find_elements_by_class_name(browser, 'webstore-de-Kb')
but the de
and Kb
part is always changing and I want to search something like webstore-[a-zA-Z]+[a-zA-Z]+-[a-zA-Z]+[a-zA-Z]+
Is it possible to do something like that? If yes, how? If no, are there any alternatives?
I'd recommend learning CSS. You can do all kinds of things. In your example, you could do something like:
a[id^='the_id']
when the element is <a href="#" id="the_id_12345" />
I detail the different types of selectors you can use here: http://ddavison.github.io/css/2014/02/18/effective-css-selectors.html
You can use:
You can execute javascript directly and have selenium return the result for you. That means you can basically find element(s) using ANY criteria, not just id or class name or even xpath.
Use the execute_script
method:
# Note: I don't know python so forgive any syntax errors.
# This example is modified from the one in the Selenium documentation:
element = driver.execute_script(
"function domTraverse (el,callback) {" +
" if (callback(el)) return el;" +
" else {" +
" var ret;" +
" for (var x=0;x<el.children.length;x++) {" +
" ret = domTraverse(el.children[x],callback);" +
" if (ret) return ret;" +
" }" +
" }" +
" return null" +
"}" +
"return domTraverse(document.body,function(e){" +
" // match desired regex:" +
" return e.className.match(/webstore-[a-zA-Z]+[a-zA-Z]+-[a-zA-Z]+[a-zA-Z]+/);" +
"});"
)
Obviously, if you have something like jQuery or YUI loaded on your page you can use them with this instead of writing your own custom DOM parsing function.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.