Skip to content Skip to sidebar Skip to footer

How To Locate The Descendant Label Elements Using Selenium And Python

I'm using Selenium python to try to find out all the descendant under the first div, so I used this code: label_element =driver.find_elements_by_xpath('//div[@style='display:block

Solution 1:

To extract all the text items from the <label> using Selenium and Python you have to induce WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    print([my_elem.textfor my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.copt[id^='coption'] div.comain>div.crow>label")))])
    
  • Using XPATH:

    print([my_elem.text for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='copt' and starts-with(@id, 'coption')]//div[@class='comain']/div[@class='crow']/label")))])
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.uiimportWebDriverWaitfrom selenium.webdriver.common.byimportByfrom selenium.webdriver.supportimport expected_conditions asEC

Solution 2:

Here is the correct xpath that will find all 3 labels.

//div[@style='display: block;']/descendant::label

enter image description here

Post a Comment for "How To Locate The Descendant Label Elements Using Selenium And Python"