Home > OS >  How to obtain value from span tag in selenium
How to obtain value from span tag in selenium

Time:07-26

The value of page_length is 100

page_length = driver.find_elements(By.CSS_SELECTOR, "section.PaginationRow-module__container___LxZJN span.PaginationRow-module__lastPage___k9Pq7")
page_len_x = page_length.get_attribute('value')
print(str(page_len_x))

This is my error message :

page_len_x = page_length.get_attribute('value')
AttributeError: 'list' object has no attribute 'get_attribute'

The tag I'm trying to fetch is :

Output

CodePudding user response:

Solution is :

page_length = driver.find_element(By.CSS_SELECTOR, "section.PaginationRow-module__container___LxZJN span.PaginationRow-module__lastPage___k9Pq7").text
print(str(page_length))

I used find_elements instead of find_element

CodePudding user response:

To print the text 100 you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span[class^='PaginationRow-module__lastPage']"))).text)
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[starts-with(@class, 'PaginationRow-module__lastPage')]"))).text)
    
  • 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
    
  • Related