Search code examples
seleniumselenium-webdriverxpathcss-selectorsgetattribute

Is there a way to identify the elements having 3 classes?


I'm stuck in a case where the element is having 3 classes and I need to get the attribute value named "data-source" and should get the value.

Can anyone help me in this?

Thanks in advance


Solution

  • To get the value of the attribute data-source of the element having 3 classes e.g. classA, classB and classC you can use you can use either of the following Locator Strategies:

    • Using and cssSelector:

      System.out.println(driver.findElement(By.cssSelector(".classA.clasB.classC")).getAttribute("data-source"));
      
    • Using and xpath:

      print(driver.find_element_by_xpath("//*[@class='classA classB classC']").get_attribute("data-source"))
      

    Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

    • Using and cssSelector:

      System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".classA.clasB.classC"))).getAttribute("data-source"));
      
    • Using and xpath:

      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[@class='classA classB classC']"))).get_attribute("data-source"))
      
    • Note : For Python you have to add the following imports :

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC