Locate an element by partial class name and return its full value using Selenium on Python

Question:

Here are two HTML code examples from two different cases: Both the examples have common partial class names: "fa fa-circle atp-", only the end is different "-green" or "red".

I’m trying to locate class by partial name and return its full name, however I failed to do it.

1st code example:

<i ng-hide="atpCtrl.atp.isLoading &amp;&amp; !hasOverrideToken" class="fa fa-circle atp-green" ng-class="cssClass" ng-if="(showAtp &amp;&amp; !requireClick) || hasOverrideToken" uib-tooltip="Available in PL51 - Pilkington AGR Sochaczew" tooltip-append-to-body="true" tooltip-placement="top" tooltip-trigger="'mouseenter outsideClick'"></i>

2nd code example:

<i ng-hide="atpCtrl.atp.isLoading &amp;&amp; !hasOverrideToken" class="fa fa-circle atp-red" ng-class="cssClass" ng-if="(showAtp &amp;&amp; !requireClick) || hasOverrideToken" uib-tooltip="Not Available" tooltip-append-to-body="true" tooltip-placement="top" tooltip-trigger="'mouseenter outsideClick'"></i>

I tried the following:

#Locate the class
availability = driver.find_element(By.CSS_SELECTOR("fa fa-circle atp-"))

#Get full class name
full_class_name = availability.get_attribute("class")
Asked By: rolandsi86

||

Answers:

To find a class using CSS_SELECTOR you need to use . or class= to mention this is a class. You also need to mention this is a partial class name using contains *=

By.CSS_SELECTOR('.fa.fa-circle[class*="atp-"]')
Answered By: Guy