Search code examples
pythonseleniumxpathcss-selectorsgetattribute

Return UID attribute value using Selenium Python


I have a site that looks like this and I want to extract the contents of the uid field using firefox + selenium + python. There is only 1 UID per page.

<div class= "parent" >
   <div class="module profile" dcp="1" uid="90">
   ...
   ...
   </div>
</div>

To make it concrete see the following:

<div class= "parent" >
   <div class="module profile" dcp="1" uid="[RETURN THIS]">
   ...
   ...
   </div>
</div>

I've tried several techniques in selenium including using

browser.find_elements_by_name
browser.find_elements_by_xpath
browser.find_elements_by_class_name
browser.find_elements_by_css_selector

But none of them are able to return the UID. I either get an empty set or I only get the class (i.e. the entire module class rather than the attributes inside the DIV).

I saw some folks recommend the get_attribute selector but I was unsuccessful in applying it to this use case.

Any guidance would be appreciated, thanks.


Solution

  • To extract the value of the attribute uid i.e. the text 90 you can use either of the Locator Strategies:

    • css_selector:

      myText = browser.find_element_by_css_selector("div.parent>div.module.profile").get_attribute("uid")
      
    • xpath:

      myText = browser.find_element_by_xpath("//div[@class='parent']/div[@class='module profile']").get_attribute("uid")
      

    However it seems the attribute uid i.e. the text 90 is a dynamic element so you have to induce WebDriverWait for the element to be visible and you can use either of the following solutions:

    • css_selector:

      myText = WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.parent>div.module.profile"))).get_attribute("uid")
      
    • xpath:

      myText = WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='parent']/div[@class='module profile']"))).get_attribute("uid")
      

    Note : 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