pythonselenium-webdriverseleniumbase

how to get text from an object in seleniumbase?


seleniumbase launches a browser, opens a page, and enters a query into the search box. Then follows the links in a loop. And then in each link at each iteration of the loop, tries to pull h3 out of the link:

def rel_prods(url, one_data):
with SB(headed=True, uc=True) as driver:
    driver.open(url)
    time.sleep(26)
    driver.type('#header-search', one_data['title'])
    driver.click('button[data-auto="search-button"]')
    time.sleep(random.uniform(2, 6))
    elements = driver.find_elements('a[data-auto="snippet-link"]')
    i = 1
    for element in elements:
        try:
            h3_tag = element.get_text('h3')
            print(h3_tag)
            element.click()
            time.sleep(2)
            driver.switch_to_window(i)
            time.sleep(random.uniform(2, 6))
            driver.close()
            driver.switch_to_default_window()
        except Exception as e:
            print(e)

And that's where the error comes out:

'WebElement' object has no attribute 'get_text'

Who knows how to implement this?

And also the driver.close() command closes the browser completely. I would like it to close only the current tab


Solution

  • The error message

    'WebElement' object has no attribute 'get_text'
    

    is telling you that there is no method .get_text() on a WebElement, e.g.

    element.get_text('h3')
    

    is not valid.

    What you want is

    element.text