0

on this page I want to parse few elements. enter image description here

I would like to get text in circles and use attribute value to click sometimes. That code returns anything. With this code I want to get all attribute values. (54,68,128,157 and 170)

 izbira_UE =  wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//ul[@id='facetUE']")))
 for ena_UE in izbira_UE:
     print(ena_UE.find_element(By.TAG_NAME, "li").get_attribute('data-val'))

But this code finds attribute of @class. That is 'cnt'. How can I get text of this element. If I add the .text() after get_attribute, I get the TypeError: 'str' object is not callable. How can I get both text (one in "a" element (name of the city) and also the number inside [](in "div" element)?

    izbira_UE = wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//*[@id='facetUE']//*[@data-val='54']")))
    for ena_UE in izbira_UE:
        print(ena_UE.find_element(By.TAG_NAME, "div").get_attribute('class'))  

Thanks for your help,

1
  • Try print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//ul[@id='facetTipi']//li[.//div[@class='cnt']]//a"))).get_attribute("data-val")) Commented Sep 4 at 2:28

1 Answer 1

1

If you're using seleniumbase, it's quite straightforward. Here's how you can get those data points:

from seleniumbase import SB

with SB(uc=True, test=True) as sb:
    sb.maximize_window()
    url = "https://www.nepremicnine.net/oglasi-prodaja/gorenjska/stanovanje/"
    sb.activate_cdp_mode(url)
    sb.uc_gui_click_captcha()

    # accept cookies
    sb.find_element("#CybotCookiebotDialogBodyButtonAccept").click()

    # find the target element
    sb.find_element("#facetUE")
    links = sb.find_elements("#facetUE>li>a")

    # Get all anchor links and data inside under #facetUE
    data = []
    for link in links:
        data_dict = {"data_val": link['data-val'], "cnt_text": link.text.split()[0], "region": link.text.split()[1]}
        data.append(data_dict)

    print(data)

output:

# data
[
  {
    'data_val': '54',
    'cnt_text': '[55]',
    'region': 'Jesenice'
  },
  {
    'data_val': '68',
    'cnt_text': '[114]',
    'region': 'Kranj'
  },
  {
    'data_val': '128',
    'cnt_text': '[26]',
    'region': 'Radovljica'
  },
  {
    'data_val': '157',
    'cnt_text': '[15]',
    'region': 'Škofja'
  },
  {
    'data_val': '170',
    'cnt_text': '[17]',
    'region': 'Tržič'
  }
]
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks again..I also solved with my code:)
izbira_UE = wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//*[@id='facetUE']/li/a"))) #//div[@class='cnt']"))) for ena_UE in izbira_UE: print(ena_UE.text.split()) #get_attribute('data-val'))
you could add it in question - it will be more visible for others.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.