Compound class names not permitted error Webdriver

后端 未结 6 671
迷失自我
迷失自我 2020-11-28 07:31

I am getting the following error:

\"Compound class names not permitted\"

While trying to access web element where-in the elemen

相关标签:
6条回答
  • 2020-11-28 07:59

    You can use

    driver.findElement(By.className("alert-success"));
    

    or

    driver.findElement(By.className("alert"));
    

    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.

    0 讨论(0)
  • 2020-11-28 08:07

    Most of the time class name attribute of any element has groups of classes names. like class = 'alert alert-success another class name'

    if you are using css selector ,then just remove spaces between the class names and create you clas name like below : driver.findElement(By.cssSelector(".alert.alert-success.another.class.name")

    . => represent class

    => represent ID

    for CSS selector.

    0 讨论(0)
  • 2020-11-28 08:10

    If usage of class name is must you can use the following ways:

    1) css selectors:

    driver.findElement(By.cssSelector(".alert.alert-success");
    

    2) using xpath

    driver.findElement(By.xpath("//div[@class='alert alert-success']"))
    

    Try avoiding xpath and use css selectors instead.

    0 讨论(0)
  • 2020-11-28 08:17

    It can also be done using class name like:

    driver.find_element_by_class_name("alert")
    

    or

    driver.find_element_by_class_name("alert-success")
    

    you can select anyone class name from two or more class names separated by spaces it'll work just fine.

    0 讨论(0)
  • 2020-11-28 08:22

    You can access the element if it has multiple classes using "By":

    from selenium.webdriver.common.by import By
    driver.findElement(By.cssSelector(".alert.alert-success"));
    
    0 讨论(0)
  • 2020-11-28 08:25

    The issue is because of the way find by Class name works.

    in your code class name is class="alert alert-success"

    If the class name has space you'll get the above error. You can simply get rid of the issue by using Id, CSS, Xpath, regular expression or any other element finder method.

    Do you need to use Class Name or can you use another method? Let me know if you need to use class name.

    0 讨论(0)
提交回复
热议问题