-1

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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • please provide selenium script you tried.. – murali selenium Oct 20 '18 at 04:39
  • The entire script don't have a lot of useful information. I believe I only need one line of selector code to get this. However I tried using things such as `browser.find_elements_by_class_name("module")[0]` but this returns the ENTIRE
    to
    . Also tried `browser.find_elements_by_xpath("module.profile")` but that again returns the children of the div instead of the attributes inside the initial div block. Thanks
    – Mario Roberto Oct 20 '18 at 05:09

1 Answers1

1

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
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352