1

Right now I'm working on a program that takes user input of questions and answers, separates them into separate lists of q's and a's, then automatically answers the question given either the question or answer. Since the place where the 'bot' is being used is online, I'm using the Selenium web driver, which is causing me some problems when trying to read an aria-label. I don't know what I'm doing wrong, as I'm not advanced at all with selenium, HTML, or CSS. I'm trying to find the aria-label value for each container without knowing what it is

An example of the HTML I'm trying to get the text value of:

<div class="MatchModeQuestionGridBoard-tile"><div class="MatchModeQuestionGridTile" touch-action="auto"><div class="MatchModeQuestionGridTile-content"><div aria-label="to cloak; to conceal the truth; to offer lame excuses" class="FormattedText notranslate TermText MatchModeQuestionGridTile-text lang-en" style="font-size: 14px;"><div style="display: block;">to cloak; to conceal the truth; to offer lame excuses</div></div></div></div></div>

Snippet of my code:

def driver():
    driver = webdriver.Chrome()
    driver.get(link)
    startMatch = driver.find_element_by_xpath("/html/body/div[5]/div/div/div/div[2]/button").click()
   
    #find text in matches
    container = driver.find_elements_by_class_name('MatchModeQuestionGridTile-content')
    containerFile = open("QuizletTerms.txt", "w+")

    for _ in list(container):
        arialabel = driver.find_elements_by_css_selector("div[aria-label='']")
        containerFile.write("\n")
        containerFile.write(str(arialabel))
        print(arialabel)

    containerFile.close()
    print("done")
    sleep(5)

Output:


[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Zayd
  • 63
  • 1
  • 7

1 Answers1

1

The texts e.g. to cloak; to conceal the truth; to offer lame excuses is present in child <div> as well in it's parent <div>. So extract it you need to induce WebDriverWait for visibility_of_all_elements_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR and get_attribute():

    print([my_elem.get_attribute("aria-label") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.MatchModeQuestionGridTile-content>div[aria-label]")))])
    
  • Using XPATH and get_attribute():

    print([my_elem.get_attribute("aria-label") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='MatchModeQuestionGridTile-content']/div[@aria-label]")))])
    
  • 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