2

I am having a display button in my GUI that shows the connection status (Button with Green check means connection is established and with Red cross means no connection) I have to check the status using my code. I am parsing the content of that particular title-bar class name (container-fluid). And from this, I am parsing the explicit content of that display button.

elem = driver.find_element_by_class_name("container-fluid")
a= elem.get_attribute("outerHTML")
b= a.split("powerOn icon-ok-sign") 

After this, I parse some explicit content of that button and decide that connection is there or not.

But If I use class="powerOn icon-ok-sign", I get error :

Compound class names not permitted

<div class="powerOn icon-ok-sign" data-original-title="Connection" style=" font-size: 2em;" data-toggle="tooltip" title="" data-placement="bottom" ng-class="{&quot;powerOn icon-ok-sign&quot;: titleArea.systemStatus.connection.value, &quot;powerOff icon-remove-sign&quot; : !titleArea.systemStatus.connection.value}"></div>
Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73

2 Answers2

1

But If I use class="powerOn icon-ok-sign", I get error Compound class names not permitted

Actually selenium doesn't support to locate an element using Compound class name.

You should try using on of then instead as :-

driver.find_element_by_class_name("powerOn")

Or

driver.find_element_by_class_name("icon-ok-sign")

Or best way to use css_selector to locate same element using multiple class name as :-

driver.find_element_by_css_selector(".powerOn.icon-ok-sign")

Reference link :-

Community
  • 1
  • 1
Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • Hi @Saurabh Gaur , I have one more doubt related to this. When my connection button shows cross over it the browser window is no more active. Thus, at that time when I use elem = driver.find_element_by_css_selector(".powerOn.icon-ok-sign") , I get this exception NoSuchElementException: Message: Unable to locate element: {"method":"css selector","selector":".powerOn.icon-ok-sign"}. So, what is the prorper way to check this condition and my script continues wiothout any exception error – Karan Patel Jan 16 '17 at 06:02
  • 1
    @KaranPatel you should use then `driver.find_elements_by_css_selector(".powerOn.icon-ok-sign")` which will return either list of element or empty list. It will never throw exception if element is not there. so you just check for empty list to determine your condition. Thanks..:) – Saurabh Gaur Jan 16 '17 at 12:53
  • I have used **try and except** condition but this also looks good. Thanks :) – Karan Patel Jan 17 '17 at 05:19
1

You can use search by CSS Selector instead:

driver.find_element_by_css_selector(".powerOn.icon-ok-sign")

or use one of class names to select your element:

driver.find_element_by_class_name("powerOn")
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • Hi Andersson, I have one more doubt related to this. When my connection button shows cross over it the browser window is no more active. Thus, at that time when I use **elem = driver.find_element_by_css_selector(".powerOn.icon-ok-sign")** , I get this exception **NoSuchElementException: Message: Unable to locate element: {"method":"css selector","selector":".powerOn.icon-ok-sign"}**. So, what is the prorper way to check this condition and my script continues wiothout any exception error. – Karan Patel Jan 16 '17 at 05:56