Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Compound class names not permitted

I have the below code that clicks on an element to pop up a screen and copy the text in it

el1 = driver.find_element_by_id("keyDev-A") el1.click() el2 = driver.find_element_by_class_name("content") print(el2.text) 

However, when I tried to get selenium to click on the button within that popup with

el3 = driver.find_element(By.CLASS_NAME, "action-btn cancel alert-display") el3.click() 

It produces an error message:

invalid selector: Compound class names not permitted 

This is the HTML that I am trying to get selenium to click on. The Close button.

<div class="nav">     <span class="action-btn confirm prompt-display">Confirm</span>     <span class="action-btn cancel prompt-display">Cancel</span>     <span class="action-btn cancel alert-display">Close</span> </div> 

How should I be writing el3 in order to click on the Close button?

like image 763
jake wong Avatar asked Jun 12 '16 07:06

jake wong


People also ask

What is compound class name in selenium?

As right now selenium doesn't support multiple class name. If your class name includes a space, WebDriver will see it as a "compound selector". You can use cssSelector or id for selecting the webelement.

What is invalid selector exception in selenium?

The invalid selector error is a WebDriver error that occurs when an element retrieval command is used with an unknown web element selector strategy. The available selector strategies are CSS, link text, partial link text, tag name, and XPath. Any other selector strategy is rejected with this error.

How do you write cssSelector in selenium?

Type “css=input[type='submit']” (locator value) in Selenium IDE. Click on the Find Button. The “Sign in” button will be highlighted, verifying the locator value. Attribute: Used to create the CSS Selector.


2 Answers

Leon's comment leads to the correct information that compound class names are no longer supported. What you could do instead is try using css selectors. In your case, the following line of code should help you get the element you want :

el3 = driver.find_element_by_css_selector(".action-btn.cancel.alert-display") 

It finds the element with all three classes (action-btn, cancel and alert-display) in the class attribute. Do note that the order of the classes does not matter here and any of the classes may appear anywhere in the class attribute. As long as the element has all three classes, it will be selected. If you want the order of the classes to be fixed, you can use the following xpath :

el3 = driver.find_element_by_xpath("//*[@class='action-btn cancel alert-display']")  
like image 196
sagarwadhwa1 Avatar answered Sep 22 '22 05:09

sagarwadhwa1


I am late to this question. But I also found a work around by treating the compound classes as a String, using tag_name, and get_attribute('class'), when you are not familiar with Xpath. It needs some more lines of code but it's straight forward and fit for beginners like me.

   elements = driver.find_elements_by_tag_name('Tag Name Here')         for element in elments:             className = watchingTable.get_attribute('class')             print(className)                 if className == 'Your Needed Classname':                     #Do your things 
like image 39
William Tong Avatar answered Sep 22 '22 05:09

William Tong