Home > Software design >  BeautifulSoup: How to access text in nested Span
BeautifulSoup: How to access text in nested Span

Time:09-27

I am new to Selenium, and I want to get the string "United States" from a enter image description here

I am confused as to how to access the span for country, as there is another span with the exact same class shown below. enter image description here

My code:

s=Service(ChromeDriverManager().install())

driver = webdriver.Chrome(service=s)
   
CLASS_NAME = "class name"
if (driver.find_elements(By.CLASS_NAME, "css-1gzpoyq e1wnkr790")!=None):
        print(driver.find_elements(By.CLASS_NAME, "css-1gzpoyq e1wnkr790").text)

The output printed is AttributeError: 'list' object has no attribute 'text', and I'm unsure why that is so.

CodePudding user response:

Try to use xpath expression.As the following expression select single element, so use .find_element instead of .find_elements

print(driver.find_element(By.XPATH, '(//span[@])[2]/span[1]').text)

CodePudding user response:

I want to get the string "United States"

Simply adjust your CSS SELECTOR use .find_element to select only one / the first element.

Following selector goes for for sibling <div> of elemetn with id="cmp-Select-Location-label" and selects the <span> by its attribute cmp-Select-Location-label:

...
url = 'https://www.indeed.com/cmp/Quintal-Contracting/reviews'
driver.get(url)
driver.find_element(By.CSS_SELECTOR, '#cmp-Select-Location-label   div [data-testid="selected-value"]').text

You may think it looks a bit complecated but I recommend to select by more static information like identifiers or HTML structure instead of dynamic classes

Output:

United States
  • Related