1

I'm trying to locate the button element "Follow" because it contains the text of the user that you're going to follow.

Element:

<div aria-label="Follow @Visiongeo" role="button" tabindex="0" class="css-18t94o4 css-1dbjc4n r-42olwf r-sdzlij r-1phboty r-rs99b7 r-15ysp7h r-4wgw6l r-1ny4l3l r-ymttw5 r-o7ynqc r-6416eg r-lrvibr" data-testid="1676648952-follow" style="background-color: rgb(239, 243, 244);"><div dir="auto" class="css-901oao r-1awozwy r-6koalj r-18u37iz r-16y2uox r-37j5jr r-a023e6 r-b88u0q r-1777fci r-rjixqe r-bcqeeo r-q4m81j r-qvutc0" style="color: rgb(15, 20, 25);"><span class="css-901oao css-16my406 css-bfa6kz r-poiln3 r-1b43r93 r-1cwl3u0 r-bcqeeo r-qvutc0"><span class="css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0">Follow</span></span></div></div>

But the thing is, the text after Follow, which is @Visiongeo changes, so I don't want to particularly locate the element with specific text.

Instead, I want to locate this element, and then get the text that starts with "@" and write it to a text file.

My code:

for followers in wait.until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(@aria-label, 'Follow')]"))):
    file = open("testfile.txt", "a")
    file.write(followers)
    file.write("\n")
    file.close()

I get TimeoutException message when I use the above code. I think I'm way off from what I intend to do.

Cassano
  • 253
  • 5
  • 36

1 Answers1

1

To locate the element with aria-label as Follow @Visiongeo you can use either of the following Locator Strategies:

  • xpath 1:

    //div[starts-with(@aria-label, 'Follow') and contains(@data-testid, 'follow')]
    
  • xpath 2:

    //div[starts-with(@aria-label, 'Follow') and contains(@data-testid, 'follow')][//span[text()='Follow']]
    
  • xpath 3:

    //div[starts-with(@aria-label, 'Follow') and contains(@data-testid, 'follow')]//span[text()='Follow']//ancestor::div[1]//ancestor::div[1]
    

Inducing WebDriverWait for the visibility_of_all_elements_located() you can use:

for followers in wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//div[starts-with(@aria-label, 'Follow') and contains(@data-testid, 'follow')]"))):

To parse the names of the followers you can use:

for followers in [my_elem.get_attribute("aria-label").split("@")[1] for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[starts-with(@aria-label, 'Follow') and contains(@data-testid, 'follow')]")))]:
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352